返回Python SQL查询“非类型”对象
我有以下用于查询的python代码:返回Python SQL查询“非类型”对象,python,mysql,sql,python-3.x,Python,Mysql,Sql,Python 3.x,我有以下用于查询的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()
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.executeSELECT*自customerDetails.fetchall
AttributeError:“非类型”对象没有属性“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()
文件:
AttributeError:“非类型”对象没有属性“fetchall”
这意味着:
cur.executeSELECT*FROM customerDetails返回None,并且没有fetchall属性
您必须在光标cur上使用fetchall方法。fetchall。execute不会返回任何内容。对游标对象本身调用fetchall!