Python MySQL Connector - Unread Result Found When Using Fetchone

Python MySQL connector - unread result found when using fetchone

All that was required was for buffered to be set to true!

cursor = cnx.cursor(buffered=True)

The reason is that without a buffered cursor, the results are "lazily" loaded, meaning that "fetchone" actually only fetches one row from the full result set of the query. When you will use the same cursor again, it will complain that you still have n-1 results (where n is the result set amount) waiting to be fetched. However, when you use a buffered cursor the connector fetches ALL rows behind the scenes and you just take one from the connector so the mysql db won't complain.

MySQL Unread Result with Python

Using MySQL Connector/Python, the Unread results found might happen when you use the connection object in different places without reading the result. It's not something one can go around. You can use the buffered option to read result immediately.

As mentioned in the comments, it's best to split the statements and execute them separately.

If you want to execute multiple statements, you'll need to use the multi=True option for the MySQLCursor.execute() method (since Connector/Python v1.0.4). Actually, if you don't use the multi option and send multiple statements, an InterfaceError will raise. (I do suspect a bug here as well..)

Additional remarks:

  • Instead of executing the USE-command to change databases, you can MySQLConnection.database property.
  • You best group the changes into one ALTER TABLE statement, like this:

    ALTER TABLE t1 DROP PRIMARY KEY, ADD id INT NOT NULL AUTO_INCREMENT KEY FIRST, ADD INDEX(c1)

MySQL Unread Result Found When Reusing Cursor Twice

I had the same issue recently if I am understanding the problem correctly. To make the cursor capable of executing multiple times, you can add the buffered attribute to the cursor and set it to true. You can add it like this:



cursorVariable = database.cursor(buffered = True)

I hope this solved your problem and good luck!



Related Topics



Leave a reply



Submit