时间戳 - 在字符串格式化 psycopg2 期间并非所有参数都已转换
Timestamp - Not all arguments converted during string formatting psycopg2
我目前正在尝试编写一个在执行时包含时间戳的查询。
我的数据库是这样的:
- ID - 2.text_data - 3.userid - 4 time_stamp(数据类型为“带时区的时间戳”)
我的查询如下所示:
try:
postgres_insert_query = """INSERT INTO tweets (text, userid) VALUES (%s, %s,) (dt,)"""
insertion_data = (text, id, dt)
cursor.execute(postgres_insert_query, insertion_data)
connection.commit()
except (Exception, psycopg2.Error) as error:
print("failed", error)
finally:
if connection:
cursor.close()
connection.close()
print("closed")
但是,当我 运行 它时,我收到错误消息“并非所有参数都在字符串格式化期间转换”
我在这里错过了什么?
编辑:我的问题被禁止了,因为我的问题没有被投票,但得到了回答,并且在评论中进行了一些很好的讨论。但是堆栈交换团队告诉我编辑我的问题以使它们更清楚,但他们收到了答案,所以我认为它们很清楚。我不是母语人士,所以我的英语不是最好的,我不能很好地表达自己。但是他们告诉我编辑它们,使它们看起来像新的并且可以被投票。我不明白为什么这有道理,但也许它解除了我的问题禁令。
你有3个字符串参数,但只有两个字符串插值标记。
postgres_insert_query = """INSERT INTO tweets (text, userid) VALUES (%s, %s,) (dt,)"""
只有两个 %s
占位符,但您随后尝试添加 3 个值。
insertion_data = (text, id, dt)
也许你想要(不看你的 table 很难知道)。
postgres_insert_query = """INSERT INTO tweets (text, userid, dt) VALUES (%s, %s, %s)"""
我目前正在尝试编写一个在执行时包含时间戳的查询。
我的数据库是这样的:
- ID - 2.text_data - 3.userid - 4 time_stamp(数据类型为“带时区的时间戳”)
我的查询如下所示:
try:
postgres_insert_query = """INSERT INTO tweets (text, userid) VALUES (%s, %s,) (dt,)"""
insertion_data = (text, id, dt)
cursor.execute(postgres_insert_query, insertion_data)
connection.commit()
except (Exception, psycopg2.Error) as error:
print("failed", error)
finally:
if connection:
cursor.close()
connection.close()
print("closed")
但是,当我 运行 它时,我收到错误消息“并非所有参数都在字符串格式化期间转换”
我在这里错过了什么?
编辑:我的问题被禁止了,因为我的问题没有被投票,但得到了回答,并且在评论中进行了一些很好的讨论。但是堆栈交换团队告诉我编辑我的问题以使它们更清楚,但他们收到了答案,所以我认为它们很清楚。我不是母语人士,所以我的英语不是最好的,我不能很好地表达自己。但是他们告诉我编辑它们,使它们看起来像新的并且可以被投票。我不明白为什么这有道理,但也许它解除了我的问题禁令。
你有3个字符串参数,但只有两个字符串插值标记。
postgres_insert_query = """INSERT INTO tweets (text, userid) VALUES (%s, %s,) (dt,)"""
只有两个 %s
占位符,但您随后尝试添加 3 个值。
insertion_data = (text, id, dt)
也许你想要(不看你的 table 很难知道)。
postgres_insert_query = """INSERT INTO tweets (text, userid, dt) VALUES (%s, %s, %s)"""