我正在尝试将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 ])
Run Code Online (Sandbox Code Playgroud)
是否可以在不使用“命名”参数(%(value)s)的情况下给出“简单”值列表/元组?