MySQL连接器方法“fetchone”和“fetchmany”不是PEP 249 complian

2024-05-08 19:45:49 发布

您现在位置:Python中文网/ 问答频道 /正文

对于Windows 10上的Python 3.6.2MySQL Connector 2.1.6包,不调用数据库游标的execute方法,或在SELECT语句(CREATEDROPALTERINSERTDELETEUPDATE等)会产生以下结果:

>>> import mysql.connector
>>> session = mysql.connector.connect(user = "root", database = "mysql")
>>> cursor = session.cursor()
>>> cursor.fetchone()
>>> cursor.fetchmany()
[]
>>> cursor.fetchall()
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "C:\Users\Maggyero\AppData\Local\Programs\Python\Python36-32\lib\site-packages\mysql\connector\cursor.py", line 891, in fetchall
    raise errors.InterfaceError("No result set to fetch from.")
mysql.connector.errors.InterfaceError: No result set to fetch from.
>>> cursor.execute("CREATE TABLE test (x INTEGER)")
>>> cursor.fetchone()
>>> cursor.fetchmany()
[]
>>> cursor.fetchall()
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "C:\Users\Maggyero\AppData\Local\Programs\Python\Python36-32\lib\site-packages\mysql\connector\cursor.py", line 891, in fetchall
    raise errors.InterfaceError("No result set to fetch from.")
mysql.connector.errors.InterfaceError: No result set to fetch from.

PEP 249显式声明fetchonefetchmanyfetchall方法:

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.

那么为什么fetchone和{}引发一个类似^{的异常呢?


Tags: tonoinconnectorlinemysqlresultcall