2017-05-26 54 views
0

鉴于Postgres的查询转换:postgres的:不是所有的参数期间字符串格式化

INSERT INTO net.bgp_communities (comm_name, comm_value) VALUES (%s, %s) ; 

('blabla', 'target:*:*') 

所得查询(使用mogrify)数据

INSERT INTO net.bgp_communities (comm_name, comm_value) VALUES ('blabla', 'target:*:*') ; 

其导致

TypeError('not all arguments converted during string formatting',) 

我相信错误来自最后没有,的元组?但这怎么解决呢?

编辑:

描述如何查询与使用元组psycopg2一起呈现

curs.executemany("""INSERT INTO net.bgp_communities (comm_name, comm_value) VALUES (%s, %s)""", msg) 
+2

只有当元组只有一个元素时,才需要''元组末尾的','。例如。 '('a',)'是一个元组,'('a')'不是。 – Kendas

+0

谢谢!任何想法谁给出如何'psycopg2'渲染查询? – iamsterdam

+0

请给出产生错误的'mogrify'的确切电话。 – chepner

回答

0

在评论解决:
注意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')) 

应该是不正确的。 - Kendas

相关问题