Code
对于游标查询:
import mysql.connector
from mysql.connector import errorcode
config = {
'user': 'root',
'password': 'root',
'host': 'localhost',
'database': 'myDatabase'}
cn = mysql.connector.connect(**config)
cur = cn.cursor()
emailExist = cur.execute("""SELECT * FROM customerDetails""").fetchall()
print(emailExist)
我的数据库文件与此脚本位于同一目录中。
属性错误
我收到此错误:
>>> emailExist = cur.execute("""SELECT * FROM customerDetails""").fetchall()
>>> AttributeError: 'NoneType' object has no attribute 'fetchall'
预期
当我在MySQLWorkbench中运行相同的查询时,输出为:
0 row(s) returned
请参阅文档 MySQL :: MySQL Connector/Python 开发人员指南 :: 10.5.6 MySQLCursor.fetchall(( 方法。
问题
错误消息
属性错误:"NoneType"对象没有属性"fetchall">
对于声明
cur.execute("""SELECT * FROM customerDetails""")
读:
- 返回的执行语句
None
- 并且
None
的返回值没有fetchall
属性或方法
修复
按以下顺序执行游标方法:
cur.execute(sql)
创建游标(以cur
为单位,而不是作为返回值(cur.fetchall()
返回结果