在Windows 10上使用Python 3.6.2和MySQL Connector 2.1.6包,不调用数据库游标的execute
方法,或在非 {{1}上调用它语句(SELECT
,CREATE
,DROP
,ALTER
,INSERT
,DELETE
等)会产生以下结果:
UPDATE
PEP 249明确说明了>>> 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.
,fetchone
和fetchmany
方法:
如果先前对.execute *()的调用未产生任何结果集或尚未发出任何调用,则会引发错误(或子类)异常。
那么为什么不fetchall
和fetchone
引发fetchmany
之类的异常?