我试图使用psycopg2 executemany一个简单的多插入,但我只能使其工作字典使用,而不是“普通”值序列:psycopg2 executemany与简单列表?
# given:
values = [1, 2, 3] ; cursor = conn.cursor()
# this raises TypeError: 'int' object does not support indexing:
cursor.executemany('INSERT INTO t (col_a) VALUES (%s)', values)
# I also tried encapsulating my 'values' into a tuple/list but it gives another exception (TypeError: not all arguments converted during string formatting).
# while this is ok:
cursor.executemany('INSERT INTO t (col_a) VALUES (%(value)s)', [ dict(value=v) for v in values ])
是不是可能给“简单“的值列表/元组而不使用”named“参数(%(value)s)?
好在谢谢Janne&Fog我现在看到它。 – gst