psycopg / psycopg2

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

SELECT queries are not working, however INSERT works #1684

Closed MathiasG-2159628 closed 7 months ago

MathiasG-2159628 commented 7 months ago
        select_query = "SELECT * from rri"
        res = connection.cursor().execute(select_query)
        result_rows = connection.cursor().fetchall()

After using the following query , I receive the error: psycopg2.ProgrammingError: no results to fetch. However there are clearly records present in the database table.

image

Insert operations work just fine.

   insert_query = "INSERT INTO rri (timestamp, ms) VALUES (%s, %s)"
    connection.cursor().execute(insert_query, (timestamp, ms))
    connection.commit()

I really appreciate if someone can reach out because I'm at my wit's end.

MathiasG-2159628 commented 7 months ago

solved: needed to assign connection.cursor() to a variable:

connection = psycopg2.connect(**db_params)
    cur = connection.cursor()
    cur.execute('SELECT * FROM rri;')

    rows = cur.fetchall()
    connection.commit()
    connection.close()