相关疑难解决方法(0)

psycopg2:使用一个查询插入多行

我需要用一个查询插入多行(行数不是常量),所以我需要像这样执行查询:

INSERT INTO t (a, b) VALUES (1, 2), (3, 4), (5, 6);
Run Code Online (Sandbox Code Playgroud)

我知道的唯一方法是

args = [(1,2), (3,4), (5,6)]
args_str = ','.join(cursor.mogrify("%s", (x, )) for x in args)
cursor.execute("INSERT INTO t (a, b) VALUES "+args_str)
Run Code Online (Sandbox Code Playgroud)

但我想要一些更简单的方法.

python postgresql psycopg2

121
推荐指数
7
解决办法
9万
查看次数

标签 统计

postgresql ×1

psycopg2 ×1

python ×1