I want to insert datetime into postgres via python
from datetime import datetime, timedelta
start_time_format = datetime.now() - timedelta(hours =33)
start_time = start_time_format.strftime("%Y-%m-%d %H:%M")
conn = psycopg2.connect(...*connect postgresql*..)
cursor = conn.cursor()
cursor.execute("""INSERT INTO table01(start_time_str) VALUES(%s);""", (start_time))
conn.commit()
I get the following error:
cursor.execute("""INSERT INTO table01(start_time_str) VALUES(%s);""", (start_time))
TypeError: not all arguments converted during string formatting
I try to change other DataType(text、character、timestamp without time zone. ...)
But still Error:(
execute()is supposed to be a tuple. And even though you have(start_time)in parentheses, that does not make it a tuple. You need(start_time,)to make it a tuple.(start_time)is not correct. If you want a single element tuple it needs to be(start_time,). Note the ','.