Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/string/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python postgres:并非所有参数都在字符串格式化期间转换_Python_String_Postgresql - Fatal编程技术网

Python 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

给定Postgres查询:

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'))