1

I am using the python mysql connector in a little script. The problem I'm facing is: when executing a select statement that returns 0 rows, I'm unable to close the cursor. When closing the cursor, "mysql.connector.errors.InternalError: Unread result found" is triggered. However, calling fetchall() results in an "mysql.connector.errors.InterfaceError: No result set to fetch from." error.

So basically, I'm unable to close the cursor because of some unread data and I'm unable to read any data because there is no data to read.

2
  • 1
    How about using try/except? Commented Dec 12, 2016 at 12:49
  • Then it complains the next time I use the connection to open a new cursor. Commented Dec 12, 2016 at 12:53

3 Answers 3

1

What about:

if cur.rowcount:
    cur.fetchall()
else:
    #whatever you want to do
Sign up to request clarification or add additional context in comments.

1 Comment

That doesn't help with cleanly closing the cursor.
1

It's been a while to this question but I'll post the solution I found anyway.

Using the official mysql connector for python, the rows are not fetched from the server until requested. As a result, if there are remaining rows, trying to close the connection throws an exception. One option is to use buffered cursor. A buffered cursor reads all rows from the server and the cursor can be closed at any point. However, this method comes with a memory cost.

There is a hard limit of one open cursor per connection for the mysql connector. Trying to open another cursor raised an exception.

Comments

0

I use this logic for dealing with empty SQL's:

import MySQLdb
cursor = db.cursor()
sql = "SELECT * FROM foobar"
dta = cursor.execute(sql)
if dta == 0:
    print "0 MySQL Entries found"
    cursor.close()

assuming I understood your question correctly, you can't close the cursor for w/e reason. You can "re-position" the cursor without closing it by simply re-assigning the cursor:

cursor = db.cursor()
doStuff()

1 Comment

I'm not using "MySQLdb" but the official mysql connector. And, I cannot create a 2nd cursor without closing the current one because that just throws the same error.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.