python - 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()
解决方案
如果你的问题还是没有解决,可以考虑更换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()
这样应该可以找到你想要的结果
推荐阅读
- reactjs - React Hook useEffect 缺少依赖项,在触发状态更改的按钮单击时运行条件
- python - RuntimeError('dot : 期望两个向量具有相同的 dtype,但发现 Double 和 Float
- elasticsearch - Composer 安装错误版本的 ElasticSearch
- c# - 解析异构 JSON 对象
- sql - 如何将 SCOPE_IDENTITY 作为值返回以在 Angular 应用程序中使用
- arrays - 使用 Flutter 中的类解析 JSON 文件
- c++ - 使用哪些数据类型从用户获取字符串,然后将其与数组中的其他字符串进行比较?
- reactjs - Heroku 应用程序给出“status=404”但没有出现
- python - 使用多标准加权对 Pandas DataFrame 进行排序
- data-structures - 如何在 Coq 中实现联合查找(不相交集)数据结构?