Python 如何使用pyqt在表中显示mysql数据库中的数据?

Python 如何使用pyqt在表中显示mysql数据库中的数据?,python,mysql,database,Python,Mysql,Database,我在mysql数据库中有一些数据,我想用pyqt将其显示到表中, 这是来自数据库的查询(SELECT*from MONITORING)。 帮助我在table小部件中显示数据库的内容 以下是我的程序的源代码: from PyQt4 import QtCore, QtGui import sys import MySQLdb from form.DBConnection import Connection import MySQLdb as mdb db = Connection() myCurs

我在mysql数据库中有一些数据,我想用pyqt将其显示到表中, 这是来自数据库的查询(
SELECT*from MONITORING
)。 帮助我在table小部件中显示数据库的内容

以下是我的程序的源代码:

from PyQt4 import QtCore, QtGui
import sys
import MySQLdb
from form.DBConnection import Connection
import MySQLdb as mdb

db = Connection()
myCursor = db.name().cursor()
try:
    _fromUtf8 = QtCore.QString.fromUtf8
except AttributeError:
    def _fromUtf8(s):
        return s

try:
    _efncoding = QtGui.QApplication.UnicodeUTF8
    def _translate(context, text, disambig):
        return QtGui.QApplication.translate(context, text, disambig, _encoding)
except AttributeError:
    def _translate(context, text, disambig):
        return QtGui.QApplication.translate(context, text, disambig)

class Ui_MainWindow(object):
    def setupUi(self, MainWindow):
        MainWindow.setObjectName(_fromUtf8("MainWindow"))
        MainWindow.resize(655, 356)
        self.centralwidget = QtGui.QWidget(MainWindow)
        self.centralwidget.setObjectName(_fromUtf8("centralwidget"))
        self.tbl_anggota = QtGui.QTableWidget(self.centralwidget)
        self.tbl_anggota.setGeometry(QtCore.QRect(15, 40, 511, 192))
        self.tbl_anggota.setObjectName(_fromUtf8("tbl_anggota"))
        self.tbl_anggota.setColumnCount(5)
        self.tbl_anggota.setRowCount(0)
        item = QtGui.QTableWidgetItem()
        self.tbl_anggota.setHorizontalHeaderItem(0, item)
        item = QtGui.QTableWidgetItem()
        self.tbl_anggota.setHorizontalHeaderItem(1, item)
        item = QtGui.QTableWidgetItem()
        self.tbl_anggota.setHorizontalHeaderItem(2, item)
        item = QtGui.QTableWidgetItem()
        self.tbl_anggota.setHorizontalHeaderItem(3, item)
        item = QtGui.QTableWidgetItem()
        self.tbl_anggota.setHorizontalHeaderItem(4, item)
        self.bt_hapus = QtGui.QPushButton(self.centralwidget)
        self.bt_hapus.setGeometry(QtCore.QRect(350, 250, 75, 23))
        self.bt_hapus.setObjectName(_fromUtf8("bt_hapus"))
        MainWindow.setCentralWidget(self.centralwidget)
        self.menubar = QtGui.QMenuBar(MainWindow)
        self.menubar.setGeometry(QtCore.QRect(0, 0, 655, 21))
        self.menubar.setObjectName(_fromUtf8("menubar"))
        MainWindow.setMenuBar(self.menubar)
        self.statusbar = QtGui.QStatusBar(MainWindow)
        self.statusbar.setObjectName(_fromUtf8("statusbar"))
        MainWindow.setStatusBar(self.statusbar)

        self.retranslateUi(MainWindow)
        QtCore.QMetaObject.connectSlotsByName(MainWindow)

    def retranslateUi(self, MainWindow):
        MainWindow.setWindowTitle(_translate("MainWindow", "MainWindow", None))
        item = self.tbl_anggota.horizontalHeaderItem(0)
        item.setText(_translate("MainWindow", "nim", None))
        item = self.tbl_anggota.horizontalHeaderItem(1)
        item.setText(_translate("MainWindow", "nama", None))
        item = self.tbl_anggota.horizontalHeaderItem(2)
        item.setText(_translate("MainWindow", "jabatan", None))
        item = self.tbl_anggota.horizontalHeaderItem(3)
        item.setText(_translate("MainWindow", "email", None))
        item = self.tbl_anggota.horizontalHeaderItem(4)
        item.setText(_translate("MainWindow", "nohp", None))
        self.bt_hapus.setText(_translate("MainWindow", "Hapus", None))


if __name__ == "__main__":
    app = QtGui.QApplication(sys.argv)
    MainWindow = QtGui.QMainWindow()
    ui = Ui_MainWindow()
    ui.setupUi(MainWindow)
    MainWindow.show()
    sys.exit(app.exec_())

我就是这样做的:

import sqlite3 as lite
...
cur = self.SQLiteDB.cursor()
cur.execute("SELECT * FROM SQLTable")
allSQLRows= cursor.fetchall()
self.myTableWidget.setRowCount(len(allSQLRows)) ##set number of rows
self.myTableWidget.setColumnCount(8) ##this is fixed for myTableWidget, ensure that both of your tables, sql and qtablewidged have the same number of columns

row = 0
while True:
    sqlRow = cur.fetchone()
    if sqlRow == None:
        break ##stops while loop if there is no more lines in sql table
    for col in range(0, 8): ##otherwise add row into tableWidget
        self.myTableWidget.setItem(row, col, QtGui.QTableWidgetItem(sqlRow[col]))
    row += 1