MySQL Forums
Forum List  »  Connector/Python

Bug? InterfaceError: Failed getting warnings when executing a query with multiple statements with get_warnings
Posted by: Barend Scholtus
Date: August 09, 2019 07:35AM

I am getting InterfaceError when I enable get_warnings and execute a query with multiple statements. I can't isolate the problem to an error in my own code so suspect a bug in the connector.

Connector version is mysql-connector-python-8.0.17 but 8.0.16 has the same issue.
Python 3.7.3 (v3.7.3:ef4ec6ed12, Mar 25 2019, 22:22:05) [MSC v.1916 64 bit (AMD64)] on win32
MySQL 5.7

To demonstrate the problem:
import mysql.connector
cnx = mysql.connector.connect(host='xx',
user='xx',
password='xx',
database='xx',
use_pure=False,
get_warnings=True)

# First test, remove later
cur = cnx.cursor()
cur.execute('SELECT "a"+1')
for row in cur:
print(row)
print(cur.fetchwarnings())
cur.close()
# End first test

# Second test
cur = cnx.cursor()
for rs in cur.execute('SELECT "a"+1; SELECT 2', multi=True):
for row in rs:
print(row)
print(rs.fetchwarnings())

For the first execute(), we execute a single statement, iterate over the cursor, fetch and print warnings. Output as expected:
(1.0,)
[('Warning', 1292, "Truncated incorrect DOUBLE value: 'a'")]

For the second test, (you can remove the first test altogether), it will execute print(row) once, then an Exception happens. Output:
(1.0,)
Traceback (most recent call last):
File "C:\Program Files\Python37\lib\site-packages\mysql\connector\connection_cext.py", line 472, in cmd_query
raw_as_string=raw_as_string)
_mysql_connector.MySQLInterfaceError: Commands out of sync; you can't run this command now

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
File "C:\Program Files\Python37\lib\site-packages\mysql\connector\cursor_cext.py", line 138, in _fetch_warnings
_ = self._cnx.cmd_query("SHOW WARNINGS")
File "C:\Program Files\Python37\lib\site-packages\mysql\connector\connection_cext.py", line 475, in cmd_query
sqlstate=exc.sqlstate)
mysql.connector.errors.DatabaseError: 2014 (HY000): Commands out of sync; you can't run this command now

During handling of the above exception, another exception occurred:
....etc....

Other notes:
* If you set get_warnings to False, no error happens and fetchwarnings() returns None
* If you remove the problem from the SQL code, no error happens and fetchwarnings() returns None
* use_pure can be True or False, the only difference is a slightly different traceback
* Using fetchall() instead of for row in rs gives the same result
* Many other variations give the same error.

Did anyone encounter the same problem? How did you solve it?

What am I doing wrong?

Options: ReplyQuote




Sorry, you can't reply to this topic. It has been closed.

Content reproduced on this site is the property of the respective copyright holders. It is not reviewed in advance by Oracle and does not necessarily represent the opinion of Oracle or any other party.