【发布时间】: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