psycopg / psycopg2

PostgreSQL database adapter for the Python programming language
https://www.psycopg.org/
Other
3.35k stars 506 forks source link

psycopg2.ProgrammingError: no results to fetch #1675

Closed LinChiaWei closed 8 months ago

LinChiaWei commented 8 months ago

This is a bug tracker If you have a question, such has "how do you do X with Python/PostgreSQL/psycopg2" please write to the mailing list or open a question instead.

Please complete the following information:

Describe the bug Please let us know: no results to fetch after using"INSERT.... RETURNING"

1: what you did 2: what you expected to happen 3: what happened instead

If possible, provide a script reproducing the issue.

import psycopg2

song_detail` = [['僕...', 'あたらよ', 'https://i.scdn.co/image/ab67616d00001e029efd9858adaf5e9af4c817c9', '2024-02-17 15:54:16']]
connection = psycopg2.connect(database="postgres",user="postgres",password="postgres",host="127.0.0.1",port='5432')
cursor = connection.cursor()
cursor.executemany("INSERT INTO listened_list (song_name, artist, image_url, timestamp_column) VALUES(%s, %s, %s, %s) RETURNING song_id", song_detail)
print(cursor.fetchone()[0])
connection.commit()
connection.close()

ProgrammingError: no results to fetch

dvarrazzo commented 8 months ago

Executemany doesn't return.

dvarrazzo commented 8 months ago

Note: in psycopg 3 you can use executemany(returning=True) instead. Docs here.

LinChiaWei commented 8 months ago

Solved it. Thanks a lot!