Bug #111170 cursor.execute(multi=True) causes RuntimeError: generator raised StopIteration
Submitted: 27 May 2023 4:33 Modified: 29 May 2023 11:17
Reporter: ze pau Email Updates:
Status: Can't repeat Impact on me:
None 
Category:Connector / Python Severity:S3 (Non-critical)
Version:2.2.9 OS:Any
Assigned to: CPU Architecture:Any
Tags: connector, generator, python, StopIteration

[27 May 2023 4:33] ze pau
Description:
hapens in Python 3.9.5, with mysql-connector version 2.2.9

as said in title, doing a cursor.execute with multi=True raises StopIteration

Traceback (most recent call last):
  File "C:\Users\myself\AppData\Local\Programs\Python\Python39\lib\site-packages\mysql\connector\cursor.py", line 486, in _execute_iter
    result = next(query_iter)
StopIteration

this can be handled with catching a Runtime exception, but that's incredibly dirty and affects readability. likely performance as well.

How to repeat:
same as documentation (https://dev.mysql.com/doc/connector-python/en/connector-python-api-mysqlcursor-execute.htm...)

in my case I ran 2 select queries

Suggested fix:
https://stackoverflow.com/a/51701040
[29 May 2023 11:17] MySQL Verification Team
Hello Ze pau,

Thank you for the report and feedback.
Please note that MySQL connector version 2.2.9 is legacy version and no longer supported. I would request you to upgrade it to 8.0.33(https://dev.mysql.com/downloads/connector/python/8.0.html) and report us back if you are experiencing the issue.

My quick test confirmed that issue is no longer seen with latest MySQL Connector Python version 8.0.33.

- use auth_plugin='mysql_native_password' with MySQL Connector 2.2.9 when connecting 8.0.x server version

import mysql.connector
import sys
import platform

print("OS: {} {}".format(platform.system(), platform.release()))
print("Python:", format(sys.version))
driver  = mysql.connector
print("Driver: {} {}".format(driver.__name__, driver.__version__))

test_conn = mysql.connector.connect(user='bug111170',password='mysql123', host='127.0.0.1',database='sakila')
cur =  test_conn.cursor()

operation = 'SELECT 1; SELECT 2'
for result in cur.execute(operation, params=None, multi=True):
  if result.with_rows:
    print("Rows produced by statement '{}':".format(result.statement))
    print(result.fetchall())
  else:
    print("Number of rows affected by statement '{}': {}".format(
      result.statement, result.rowcount))

####### Connector Python 2.2.9

OS: Windows 10
Python: 3.9.10 (tags/v3.9.10:f2f3f53, Jan 17 2022, 15:14:21) [MSC v.1929 64 bit (AMD64)]
Driver: mysql.connector 2.2.9
Rows produced by statement 'SELECT 1':
[(1,)]
Rows produced by statement 'SELECT 2':
[(2,)]
Traceback (most recent call last):
  File "C:\Work\Connectors\Python\Bug111170\Bug111170\env\lib\site-packages\mysql\connector\cursor.py", line 486, in _execute_iter
    result = next(query_iter)
StopIteration

The above exception was the direct cause of the following exception:

Traceback (most recent call last):
  File "C:\Work\Connectors\Python\Bug111170\Bug111170\Bug111170.py", line 14, in <module>
    for result in cur.execute(operation, params=None, multi=True):
RuntimeError: generator raised StopIteration
Press any key to continue . . .

####### MySQL Connector Python 8.0.33

OS: Windows 10
Python: 3.9.10 (tags/v3.9.10:f2f3f53, Jan 17 2022, 15:14:21) [MSC v.1929 64 bit (AMD64)]
Driver: mysql.connector 8.0.33
Rows produced by statement 'SELECT 1':
[(1,)]
Rows produced by statement 'SELECT 2':
[(2,)]
Press any key to continue . . .

regards,
Umesh