代码
对于游标查询:
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 Developer Guide :: 10.5.6 MySQLCursor.fetchall() Method .
问题
错误消息
AttributeError: 'NoneType' object has no attribute 'fetchall'
对于声明
cur.execute("""SELECT * FROM customerDetails""")
内容为:
- 执行语句返回
None
- 并且
None
的返回值没有fetchall
属性或方法
修复
按以下顺序执行游标方法:
cur.execute(sql)
创建游标(在cur
中,而不是返回)cur.fetchall()
返回结果
关于python - 光标: AttributeError: 'NoneType' object has no attribute 'fetchall' ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59829195/