Stephen
Stephen

Reputation: 31

How to check if pyodbc cursor is empty?

using pyodbc to query a MySQL database with SELECT. I need to determine if the query returned anything or not, the way I found that people were using is the rowcount, however this always returns -1 for me after some testing. I found this on the github wiki for cursor which I think describes my problem.

rowcount The number of rows modified by the last SQL statement.

This is -1 if no SQL has been executed or if the number of rows is unknown. Note that it is not uncommon for databases to report -1 immediately after a SQL select statement for performance reasons. (The exact number may not be known before the first records are returned to the application.)

I am wondering if either there is a way around this or if there is another way to do it, thanks.

Upvotes: 3

Views: 5923

Answers (1)

Nir Elbaz
Nir Elbaz

Reputation: 626

I always check the length of the return results

res=newcursor.fetchall()
if len(res)==0:##means no results

Upvotes: 6

Related Questions