我正在尝试使用psycopg2 executemany进行简单的多插入,但我只能使用dict而不是“普通”值序列:
# 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),是不是可以给出一个“简单”的列表/元组值?
最佳答案 executemany需要一系列序列,例如.列表清单:
[[v] for v in values]