问题
I want to insert many values into a table using execute_values
. I also need the ids of the affected rows. I want to use RETURNING id
. I have set the fetch
flag. Still, the result is None.
- postgres 11
- python 3.6.9
- psycopg2 '2.8.3 (dt dec pq3 ext lo64)'
I create a table with
CREATE TABLE dummy_table (
id serial,
name text,
age int
);
The values and query are
values = [('bob', 10), ('alice', 12)]
query = 'INSERT INTO dummy_table (name, age) VALUES %s RETURNING id;'
I call execute values like this
result = psycopg2.extras.execute_values(cursor, query, values, fetch=True)
connection.commit()
print(result)
> None
What am I doing wrong?
来源:https://stackoverflow.com/questions/59085374/psycopg2-returns-none-when-execute-many-is-combined-with-returning-id