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
Differencebetween Np.Array() and Np.Asarray()
Listing Contents of a Bucket with Boto3
Python Argparse: Default Value or Specified Value
String with 'F' Prefix in Python-3.6
What Are All the Dtypes That Pandas Recognizes
How to Set Selenium Python Webdriver Default Timeout
Joining Pairs of Elements of a List
Turning Off Logging in Selenium (From Python)
Subclassing Python Dictionary to Override _Setitem_
Can't Use '\1' Backreference to Capture-Group in a Function Call in Re.Sub() Repr Expression
Writing List of Strings to Excel CSV File in Python
How to Have Shared Log Files Under Windows
Dump to JSON Adds Additional Double Quotes and Escaping of Quotes
How to Read Class Attributes in the Same Order as Declared
How to Test a Function with Input Call
Is 'Import Module' Better Coding Style Than 'From Module Import Function'