简体   繁体   English

MySQL python 连接器在 select 查询中返回空结果

[英]MySQL python connector returns empty result on select query

I am trying to fetch a result from my database table users, using the cursor.execute() statement but when I try to execute the program, I get an empty result.我正在尝试使用 cursor.execute() 语句从我的数据库表用户中获取结果,但是当我尝试执行该程序时,我得到一个空结果。 Here's the code这是代码

import mysql.connector as mysql;将 mysql.connector 导入为 mysql; mycon=mysql.connect(host="localhost",user="root",passwd="12345",database="Markwiss") cursor =mycon.cursor() cursor.execute("select*from users;") mycon=mysql.connect(host="localhost",user="root",passwd="12345",database="Markwiss") cursor =mycon.cursor() cursor.execute("select*from users;")

I recently updated both python and mysql to the latest versions and since then I have been facing this problem.我最近将 python 和 mysql 更新到最新版本,从那时起我就一直面临这个问题。 I have tried executing some other statements like INSERT INTO, DELETE FROM, and they have been working fine.我已经尝试执行其他一些语句,如 INSERT INTO、DELETE FROM,它们运行良好。

Looks like you're executing the query but not reading it.看起来您正在执行查询但没有阅读它。 Try adding the following:尝试添加以下内容:

result = cursor.fetchall()

then you can access each line of the query with a for loop:然后您可以使用 for 循环访问查询的每一行:

for line in result:
        #do something ```

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM