pyqt5 tablewidgets检索数据

时间:2018-02-20 11:04:15

标签: python mysql pyqt5

你好我想从mysql数据库中检索数据,我想在我的pyqt5 tablewidget中添加它,但是它给了我一个int对象不可迭代的错误

这是我的代码

def LoadData(self):
    conn = mdb.connect("localhost", "root", "", "pyqt5")

    with conn:
        query = "SELECT * FROM data"
        cur = conn.cursor()
        result = cur.execute(query)
    self.tableWidget.setRowCount(0)
    for row_number, row_data in enumerate():
        self.tableWidget.insertRow(row_number)
        for col_number, data in enumerate(row_data):
            self.tableWidget.setItem(row_number, col_number, QtWidgets.QTableWidgetItem(str(data)))

2 个答案:

答案 0 :(得分:0)

试试这个。你缺少“枚举(结果)”到第一个for循环。

 def LoadData(self):
    conn = mdb.connect("localhost", "root", "", "pyqt5")

    with conn:
        query = "SELECT * FROM data"
        cur = conn.cursor()
        result = cur.execute(query)
    self.tableWidget.setRowCount(0)
    for row_number, row_data in enumerate(result):
        self.tableWidget.insertRow(row_number)
        for col_number, data in enumerate(row_data):
            self.tableWidget.setItem(row_number, col_number, QtWidgets.QTableWidgetItem(str(data)))

答案 1 :(得分:0)

有点晚了,但您需要进行一些编辑: cur = 执行(查询) 结果= cur.fetchall()

def LoadData(self):

conn = mdb.connect("localhost", "root", "", "pyqt5")
with conn:
    query = "SELECT * FROM data"
    cur = conn.cursor()
    cur.execute(query)
    result = cur.fetchall()
self.tableWidget.setRowCount(0)
for row_number, row_data in enumerate(result):
    self.tableWidget.insertRow(row_number)
    for col_number, data in enumerate(row_data):
        self.tableWidget.setItem(row_number, col_number, QtWidgets.QTableWidgetItem(str(data)))