Python:通过传递参数,使用python插入到postGreSQL表中

时间:2016-10-05 06:32:11

标签: python sql insert psycopg2

我有两个清单:

columns = ['id','name','address']
data = ['1,'John','LA'','2,'Jessica','TX'']

我有一个包含上述相同列的表模式,当我将值和列名称作为参数传递时,我无法使用psycopg2连接将数据插入表中。

cursor_r.execute("insert into test %s values %s ",tuple(column_names),tuple(values)))

我收到错误

psycopg2.ProgrammingError: syntax error at or near "'id'"

我真的无法弄清楚我做错了什么,真的很感激任何帮助。提前谢谢你试图帮助我。 编辑:我尝试输入此查询:

cursor_r.execute("insert into test (%s) values (%s) ", ((x for x in column_names),(str(y).replace("(","").replace(")","") for y in values)))

它现在给我一个错误,它无法适应类型'生成器'

编辑2:我现在收到的错误是' generator'对象不支持索引。有人帮帮我

1 个答案:

答案 0 :(得分:0)

INSERT需要()中的列和值,因此您需要INSERT INTO test (%s) VALUES (%s)

请参阅:http://www.w3schools.com/sql/sql_insert.asp

相关问题