返回Python SQL查询“非类型”对象

2024-10-01 09:18:41 发布

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

我有以下用于查询的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' object has no attribute '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()

Tags: 代码fromimportconfigexecuteconnectormysqlroot
1条回答
网友
1楼 · 发布于 2024-10-01 09:18:41

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

AttributeError: 'NoneType' object has no attribute 'fetchall'

这意味着:

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

必须对游标(cur.fetchall())使用fetchall()方法

相关问题 更多 >