Python postgres:并非所有参数都在字符串格式化期间转换
给定Postgres查询:Python postgres:并非所有参数都在字符串格式化期间转换,python,string,postgresql,Python,String,Postgresql,给定Postgres查询: INSERT INTO net.bgp_communities (comm_name, comm_value) VALUES (%s, %s) ; 数据呢 ('blabla', 'target:*:*') 结果查询是(使用mogrify) 导致 TypeError('not all arguments converted during string formatting',) 我相信错误来自元组末尾没有,?但如何解决这个问题呢 编辑: 描述如何使用psycopg
INSERT INTO net.bgp_communities (comm_name, comm_value) VALUES (%s, %s) ;
数据呢
('blabla', 'target:*:*')
结果查询是(使用mogrify
)
导致
TypeError('not all arguments converted during string formatting',)
我相信错误来自元组末尾没有,
?但如何解决这个问题呢
编辑:
描述如何使用psycopg2将查询与元组一起呈现
curs.executemany("""INSERT INTO net.bgp_communities (comm_name, comm_value) VALUES (%s, %s)""", msg)
已在评论中解决:请注意,
executemany
需要一组参数集-
cursor.executemany("INSERT INTO sometable (id, somefield) VALUES (%s, %s)", [(1, 'a'), (2, 'b')])
应该是正确的,而
cursor.executemany("INSERT INTO sometable (id, somefield) VALUES (%s, %s)", (1, 'a'))
应该是不正确的肯达斯
只有当元组只有一个元素时,才需要元组末尾的尾随
,
。例如,('a'),
是一个元组,('a')
不是。谢谢!考虑到psycopg2
是如何呈现查询的,你知道该逃避谁吗?请显示产生错误的对mogrify
的确切调用。@chepner谢谢!请参见edit
注意executemany
需要一个iterable参数集-cursor。executemany(“插入到某个表(id,某个字段)值(%s,%s)”,[(1,'a'),(2,'b')])
应该是正确的,而cursor则是正确的。executemany(“插入到某个表(id,某个字段)值(%s,%s)”,(1,'a'))
应该是错误的。
cursor.executemany("INSERT INTO sometable (id, somefield) VALUES (%s, %s)", (1, 'a'))