MAN*_*ANU 1 python postgresql psycopg2
我正在尝试使用此psycopg2使用 cursor.mogrify 将大量行插入 postgres :insert multiple rows with a query
data 是一个元组列表,其中每个元组是需要插入的一行。
cursor = conn.cursor()
args_str = ','.join(cursor.mogrify("(%s,%s,%s,%s,%s,%s,%s,%s)", x) for x in data)
cursor.execute(
"insert into table1 (n, p, r, c, date, p1, a, id) values " + args_str)`
Run Code Online (Sandbox Code Playgroud)
但得到错误:
TypeError: sequence item 0: expected str instance, bytes found
Run Code Online (Sandbox Code Playgroud)
在线:
args_str = ','.join(cursor.mogrify("(%s,%s,%s,%s,%s,%s,%s,%s)", x) for x in data)
Run Code Online (Sandbox Code Playgroud)
如果我尝试更改为b''.join(cursor.mogrify("(%s,%s,%s,%s,%s,%s,%s,%s)", x) for x in data ),然后执行查询给出插入字节的错误....
难道我做错了什么 ?
data = [(1,2),(3,4)]
args_str = ','.join(['%s'] * len(data))
sql = "insert into t (a, b) values {}".format(args_str)
print (cursor.mogrify(sql, data).decode('utf8'))
#cursor.execute(sql, data)
Run Code Online (Sandbox Code Playgroud)
输出:
insert into t (a, b) values (1, 2),(3, 4)
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
2223 次 |
| 最近记录: |