根据psycopg2:用一个查询插入多行 https://stackoverflow.com/questions/8134602/psycopg2-insert-multiple-rows-with-one-query,使用 psycopg2 的效率要高得多execute http://initd.org/psycopg/docs/cursor.html#cursor.execute代替执行许多 http://initd.org/psycopg/docs/cursor.html#cursor.executemany。其他人可以确认吗?
上述 StackOverflow 问题建议使用mogrify http://initd.org/psycopg/docs/cursor.html#cursor.mogrify用于创建此类语句:
INSERT INTO table VALUES (value1, value2), (value3, value4)
是否可以使用常规生成这样的语句execute http://initd.org/psycopg/docs/cursor.html#cursor.execute功能?我想到了某种形式
cursor.execute("""INSERT INTO table VALUES (%s, %s), (%s, %s)""", ((value1,value2),(value3,value4)))
会工作。
UPDATE:
例如,我尝试传递执行sql语句:
insert into history (timestamp) values (%s),(%s);
与以下元组:
(('2014-04-27 14:07:30.000000',), ('2014-04-27 14:07:35.000000',))
但我得到的只是错误:
没有可获取的结果