Detect whether to fetch from psycopg2 cursor or not?
Question:
Let’s say if I execute the following command.
insert into hello (username) values ('me')
and I ran like
cursor.fetchall()
I get the following error
psycopg2.ProgrammingError: no results to fetch
How can I detect whether to call fetchall() or not without checking the query is “insert” or “select”?
Thanks.
Answers:
Look at this attribute:
cur.description
After you have executed your query, it will be set to None if no rows were returned, or will contain data otherwise – for example:
(Column(name='id', type_code=20, display_size=None, internal_size=8, precision=None, scale=None, null_ok=None),)
Catching exceptions is not ideal because there may be a case where you’re overriding a genuine exception.
The accepted answer using cur.description
does not solve the problem any more. cur.statusmessage
can be a solution. This returns SELECT 0
or INSERT 0 1
. A simple string operation can then help determine the last query.
The problem is that what turns out to be None is the result of cur.fetchone()
So the way to stop the loop is :
cursor.execute("SELECT * from rep_usd")
output = cursor.fetchone()
while output is not None:
print(output)
output = DBCursor.fetchone()
cursor.description will never be None!
Check whether cursor.pgresult_ptr is None or not.
cursor.execute(sql)
if cursor.pgresult_ptr in not None:
cursor.fetchall()
The current best solution I found is to use cursor.rowcount
after an execute()
. This will be > 0 if the execute()
command returns a value otherwise it will be 0.
Let’s say if I execute the following command.
insert into hello (username) values ('me')
and I ran like
cursor.fetchall()
I get the following error
psycopg2.ProgrammingError: no results to fetch
How can I detect whether to call fetchall() or not without checking the query is “insert” or “select”?
Thanks.
Look at this attribute:
cur.description
After you have executed your query, it will be set to None if no rows were returned, or will contain data otherwise – for example:
(Column(name='id', type_code=20, display_size=None, internal_size=8, precision=None, scale=None, null_ok=None),)
Catching exceptions is not ideal because there may be a case where you’re overriding a genuine exception.
The accepted answer using cur.description
does not solve the problem any more. cur.statusmessage
can be a solution. This returns SELECT 0
or INSERT 0 1
. A simple string operation can then help determine the last query.
The problem is that what turns out to be None is the result of cur.fetchone()
So the way to stop the loop is :
cursor.execute("SELECT * from rep_usd")
output = cursor.fetchone()
while output is not None:
print(output)
output = DBCursor.fetchone()
cursor.description will never be None!
Check whether cursor.pgresult_ptr is None or not.
cursor.execute(sql)
if cursor.pgresult_ptr in not None:
cursor.fetchall()
The current best solution I found is to use cursor.rowcount
after an execute()
. This will be > 0 if the execute()
command returns a value otherwise it will be 0.