【问题标题】:Python SQL Query 'NoneType' object returned返回 Python SQL 查询“NoneType”对象
【发布时间】:2020-01-20 18:41:28
【问题描述】:

我有这个用于查询的 python 代码:

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' 对象没有属性 'fetchall'

我的数据库文件与此脚本位于同一目录中。

当我在 MySQLWorkbench 中运行相同的查询时,输出是:

0 行返回

为插入添加代码:

cursor = cn.cursor()
sql1 = """INSERT INTO customerDetails
VALUES (3, "h", "h",
"h", "h", "h",
"h", "h", "h",
1, 1, 1)"""
result = cursor.execute(sql1)
cursor.close()
cn.close()

【问题讨论】:

  • execute 不返回任何内容。在光标对象本身上调用fetchall

标签: python mysql sql python-3.x


【解决方案1】:

文档:https://dev.mysql.com/doc/connector-python/en/connector-python-api-mysqlcursor-fetchall.html

AttributeError: 'NoneType' 对象没有属性 'fetchall'

这意味着:

cur.execute("""SELECT * FROM customerDetails""") 返回 None 并且它没有 fetchall 属性。

您必须在光标上使用 fetchall() 方法 (cur.fetchall())。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-05-13
    • 2021-03-12
    • 2019-01-12
    • 1970-01-01
    • 2020-06-04
    • 2019-10-20
    • 1970-01-01
    • 2021-03-30
    相关资源
    最近更新 更多