With Python 3.6.2 and MySQL Connector 2.1.6 package on Windows 10, not calling the execute
method of a database cursor, or calling it on a non SELECT
statement (CREATE
, DROP
, ALTER
, INSERT
, DELETE
, UPDATE
, etc.) yields the following results:
JavaScript
x
23
23
1
>>> import mysql.connector
2
>>> session = mysql.connector.connect(user = "root", database = "mysql")
3
>>> cursor = session.cursor()
4
>>> cursor.fetchone()
5
>>> cursor.fetchmany()
6
[]
7
>>> cursor.fetchall()
8
Traceback (most recent call last):
9
File "<stdin>", line 1, in <module>
10
File "C:UsersMaggyeroAppDataLocalProgramsPythonPython36-32libsite-packagesmysqlconnectorcursor.py", line 891, in fetchall
11
raise errors.InterfaceError("No result set to fetch from.")
12
mysql.connector.errors.InterfaceError: No result set to fetch from.
13
>>> cursor.execute("CREATE TABLE test (x INTEGER)")
14
>>> cursor.fetchone()
15
>>> cursor.fetchmany()
16
[]
17
>>> cursor.fetchall()
18
Traceback (most recent call last):
19
File "<stdin>", line 1, in <module>
20
File "C:UsersMaggyeroAppDataLocalProgramsPythonPython36-32libsite-packagesmysqlconnectorcursor.py", line 891, in fetchall
21
raise errors.InterfaceError("No result set to fetch from.")
22
mysql.connector.errors.InterfaceError: No result set to fetch from.
23
PEP 249 explicitly states for the fetchone
, fetchmany
and fetchall
methods:
An Error (or subclass) exception is raised if the previous call to .execute*() did not produce any result set or no call was issued yet.
So why don’t fetchone
and fetchmany
raise an exception like fetchall
?
Advertisement
Answer
I filed a bug report on bugs.mysql.com and the bug has been fixed in MySQL Connector/Python 8.0.23.