小编gst*_*gst的帖子

psycopg2 executemany简单列表?

我正在尝试将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)的情况下给出“简单”值列表/元组?

python psycopg2 executemany

3
推荐指数
1
解决办法
9554
查看次数

标签 统计

executemany ×1

psycopg2 ×1

python ×1