首页 > 解决方案 > Python 和 MySQL - fetchall() 不显示任何结果

问题描述

在此处输入图像描述

从我的 Python 代码获取查询结果时遇到问题。与数据库的连接似乎有效,但我总是收到错误消息:

"InterfaceError: No result set to fetch from."

有人可以帮我解决我的问题吗?谢谢!!!

cnx = mysql.connector.connect(
    host="127.0.0.1" , 
    user="root" , 
    passwd="*****",
    db="testdb"
)
cursor = cnx.cursor()
query = ("Select * from employee ;")

cursor.execute(query)

row = cursor.fetchall()

标签: pythonmysqlmysql-pythonfetchall

解决方案


如果你的问题还是没有解决,可以考虑更换python mysql驱动包,使用pymysql. 你可以写这样的代码

#!/usr/bin/python
import  pymysql

db = pymysql.connect(host="localhost",    # your host, usually localhost
                     user="test",         # your username
                     passwd="test",  # your password
                     db="test")        # name of the data base

# you must create a Cursor object. It will let
#  you execute all the queries you need
cur = db.cursor()

query = ("SELECT * FROM employee")

# Use all the SQL you like
cur.execute(query)

# print all the first cell of all the rows
for row in cur.fetchall():
    print(row[0])

db.close()

这样应该可以找到你想要的结果


推荐阅读