使用 mysql.connector 模块获取 MYSQL 个表的值

get vaLues of MYSQL tables using mysql.connector module

我正在使用 mysql.connector python 库连接到 mysql。我需要使用 pyqt5 GUI 框架在 python 程序中显示数据库中存在的 table。

这是我试图打印出 table 中的行数,但即使我有 3 行,它也显示为 0。

cursor=mydb.cursor()
command = "select * from MENU"
result = cursor.execute(command)
self.tableWidget.setRowCount(cursor.rowcount)
print(cursor.rowcount)

那是因为你还没有从游标中获取任何东西。

As the docs say:

For nonbuffered cursors, the row count cannot be known before the rows have been fetched. In this case, the number of rows is -1 immediately after query execution and is incremented as rows are fetched.

所以要修复,您可以像这样获取光标:cursor=mydb.cursor(buffered=True) 或者您可以使用 results = cursor.fetchall().

等方式获取所有结果