要使用execute方法,将要插入的数据放入列表中。列表将由psycopg2调整为数组。然后取消嵌套该数组并根据需要强制转换值
import psycopg2insert = """ insert into history ("timestamp") select value from unnest(%s) s(value timestamp) returning *;"""data = [('2014-04-27 14:07:30.000000',), ('2014-04-27 14:07:35.000000',)]conn = psycopg2.connect("host=localhost4 port=5432 dbname=cpn")cursor = conn.cursor()cursor.execute(insert, (data,))print cursor.fetchall()conn.commit()conn.close()不确定与executemany的性能差异是否很大。但我认为以上方法更加整洁。
returning顾名思义,该子句将返回插入的元组。
BTW
timestamp是保留字,不应用作列名。



