获取插入psycopg2中的多行的ID [英] getting ids of multiple rows inserted in psycopg2

查看:211
本文介绍了获取插入psycopg2中的多行的ID的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想使用psycopg2来 INSERT 多行,然后返回所有 id (按顺序)使用单个查询。这就是PostgreSQL的 RETURNING 扩展名设计的,并且使用 cursor.execute 似乎可以正常工作:

  cursor.execute(
INSERT INTO my_table(field_1,field_2)
VALUES(0,0) ,(0,0)返回id;

打印cursor.fetchall()

[(1,),(2,)]

现在,为了传递动态生成的数据,似乎是 cursor.executemany 是方法:

  data = [(0,0),(0,0)] 

cursor.executemany(
INSERT INTO my_table(field_1,field_2)
VALUES(%s,%s)RETURNING id;,
数据

但是,在这种情况下, cursor.fetchall()产生以下内容:

  [(4,),(None,)] 

如何正确返回所有 id

解决方案

您不应该能够从 executemany 获得结果:


该功能对于更新数据库的命令最有用:查询返回的任何结果集都将被丢弃。


Per psycopg2文档



最好是在事务中循环单个 insert ,或使用多值 insert ...返回,尽管在​​后一种情况下,您必须小心使用另一个输入值来匹配返回的ID,但不能仅仅假设返回的ID的顺序与输入的 VALUES 列表。



当我在本地运行测试时,它只是失败了:

 >>>导入psycopg2 
>> conn = psycopg2.connect( dbname = regress)
>> curs = conn.cursor()
>> curs.execute(创建表my_table(id串行主键,field_1整数,field_2整数);)
>> data = [(0,0),(0,0)]
>> curs.executemany(
... INSERT INTO my_table(field_1,field_2)
... VALUES(%s,%s)RETURNING id;,
...数据
...)
>>
>> curs.fetchall()
追溯(最近一次调用为最后一次):
文件< stdin>,在< module>中的第1行。
psycopg2.ProgrammingError:无结果可获取

经psycopg2 2.5.1。测试。


I'd like to use psycopg2 to INSERT multiple rows and then return all the ids (in order) using a single query. This is what PostgreSQL's RETURNING extension is designed for, and it seems to work fine using cursor.execute:

cursor.execute(
    "INSERT INTO my_table (field_1, field_2) "
    "VALUES (0, 0), (0, 0) RETURNING id;"
)
print cursor.fetchall()

[(1,), (2,)]

Now, in order to pass in dynamically-generated data, it seems like cursor.executemany is the way to go:

data = [(0, 0), (0, 0)]

cursor.executemany(
    "INSERT INTO my_table (field_1, field_2) "
    "VALUES (%s, %s) RETURNING id;",
    data
)

However, in that case, cursor.fetchall() produces the following:

[(4,), (None,)]

How do I get it to correctly return all the ids instead of just the one?

解决方案

You're not supposed to be able to get results from executemany:

The function is mostly useful for commands that update the database: any result set returned by the query is discarded.

Per the psycopg2 docs.

You'll be better off looping over a single insert within a transaction, or using a multi-valued insert... returning, though in the latter case you must be careful to match returned IDs using another input value, you can't just assume the order of returned IDs is the same as the input VALUES list.

When I run your test locally, it simply fails:

>>> import psycopg2
>>> conn = psycopg2.connect("dbname=regress")
>>> curs = conn.cursor()
>>> curs.execute("create table my_table(id serial primary key, field_1 integer, field_2 integer);")
>>> data = [(0, 0), (0, 0)]
>>> curs.executemany(
...     "INSERT INTO my_table (field_1, field_2) "
...     "VALUES (%s, %s) RETURNING id;",
...     data
... )
>>> 
>>> curs.fetchall()
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
psycopg2.ProgrammingError: no results to fetch

Tested with psycopg2 2.5.1.

这篇关于获取插入psycopg2中的多行的ID的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆