Python mysql.connector行数>;0,但fetchall()不显示任何结果
我正在使用mysql.connector并运行:Python mysql.connector行数>;0,但fetchall()不显示任何结果,python,mysql,python-3.x,mysql-python,mysql-connector,Python,Mysql,Python 3.x,Mysql Python,Mysql Connector,我正在使用mysql.connector并运行: import mysql.connector from mysql.connector import errorcode def get_db_connection(user, password, database): try: return mysql.connector.connect(user=user, password=password,
import mysql.connector
from mysql.connector import errorcode
def get_db_connection(user, password, database):
try:
return mysql.connector.connect(user=user, password=password,
host='localhost',
database=database)
except mysql.connector.Error as err:
if err.errno == errorcode.ER_ACCESS_DENIED_ERROR:
print("Something is wrong with your user name or password")
elif err.errno == errorcode.ER_BAD_DB_ERROR:
print("Database does not exist")
else:
print(err)
else:
cnx.close()
db = get_db_connection('user', 'password', 'database123')
cursor = db.cursor()
query = "SELECT * FROM companies;"
cursor.execute(query)
companies_results = cursor.fetchall()
我得到的是“mysql.connector.errors.InterfaceError:No result set to fetch from.”错误是行数>0(它是2)。我知道那张表里只有两个结果
系统详细信息:
-视窗10
-Python 3.7.0
-服务器类型:MariaDB
-服务器版本:10.3-MariaDB最初我认为我已经通过向光标添加
buffered
参数解决了这个问题
cursor = db.cursor(buffered=True)
事实并非如此
在调试时,我意识到cursor.fetchall()上有一个watch变量。这是在代码中调用该命令之前运行的,这意味着当它到达代码时,我们无法获取任何结果
解决方案:没有cursor.fetchall()的监视变量。如果不进行行计数而只调用fetchall会发生什么?同样的事情不幸的是,我唯一能想到的是库中的一些兼容性问题。假设你通过pip安装了这个?也许试着用康达?