Home >Database >Mysql Tutorial >Why Does My Python MySQL Connector Throw an \'Unread Result Found\' Error When Using `fetchone()`?
Python MySQL Connector: "Unread Result Found" When Using Fetchone
When inserting parsed JSON data into a MySQL database using the Python connector, an error occurs: "Unread result found." This error is associated with a particular section of code.
In the code snippet, a series of queries is executed using the cursor.execute method, followed by a call to cursor.fetchone to retrieve a single row from the result set. However, when this code is added to a larger program, subsequent queries using the same cursor fail with the "Unread result found" error.
The error suggests that there are unhandled results from previous queries. To resolve this issue, it is recommended to exhaust the results from each query using the cursor.fetchall method. However, attempts to do this within a try-except block to handle the mysql.connector.errors.InterfaceError have proven unsuccessful.
The specific section of code causing the error is as follows:
cursor.execute(query,(travel_mode, Orig_lat, Orig_lng, Dest_lat, Dest_lng, time_stamp)) leg_no = cursor.fetchone()[0] try: cursor.fetchall() except mysql.connector.errors.InterfaceError as ie: if ie.msg == 'No result set to fetch from.': pass else: raise cursor.execute(query,(leg_travel_mode, leg_Orig_lat, leg_Orig_lng, leg_Dest_lat, leg_Dest_lng, time_stamp))
To resolve the issue, it is necessary to set the buffered parameter to True when creating the cursor. This ensures that all results from a query are fetched into a buffer before any subsequent queries are executed.
cursor = cnx.cursor(buffered=True)
With the buffered parameter set to True, the error "Unread result found" is no longer encountered.
The above is the detailed content of Why Does My Python MySQL Connector Throw an \'Unread Result Found\' Error When Using `fetchone()`?. For more information, please follow other related articles on the PHP Chinese website!