我想使用sqlite3和PyQt5在qtablewidget上显示db文件

时间:2018-08-30 18:41:26

标签: python-3.x sqlite pyqt5

我的计算机上已经有播放器数据,该数据包含8908行和3列。我想在名为“ SearchResult”的QTablewidget上显示数据。

第一栏:国籍 第二列:PlayerName 第三栏:薪金

我看了一下说要使用qtsql的帖子,并且尝试了,但是不起作用。

我还制作了名为'SearchInput'的LineEdit来搜索包含某些名称的特定播放器。例如,如果我在Lineedit上键入“ Ronal”并单击按钮,Tablewidget仅显示名称包含“ Ronal”。我不知道该怎么做。

这是我的代码。

from PyQt5 import QtCore, QtGui, QtWidgets
from PyQt5.QtSql import QSqlDatabase, QSqlQuery
import sqlite3

conn = sqlite3.connect('C:/Users/Daniel/Desktop/FO4Player.db')
c = conn.cursor() 
c.execute('pragma foreign_keys = ON;')
conn.commit()

class Ui_SquadMaker(object):
    def load_inital_data(self):
        self.c.execute('Select * FROM table FO4Player')
        rows = self.c.fetchall()

        for row in rows:
            inx = rows.index(row)
            self.SearchResult.insertRow(inx)
            self.SearchResult.setItem(inx , 0 , QTableWidgetItem(row[1]))
            self.SearchResult.setItem(inx , 0 , QTableWidgetItem(row[2]))
            self.SearchResult.setItem(inx , 0 , QTableWidgetItem(row[3]))

     def setupUi(self, SquadMaker):
            SquadMaker.setObjectName("SquadMaker")
            SquadMaker.resize(1280, 718)
            self.centralwidget = QtWidgets.QWidget(SquadMaker)
            self.centralwidget.setObjectName("centralwidget")
            self.SearchInput = QtWidgets.QLineEdit(self.centralwidget)
            self.SearchInput.setGeometry(QtCore.QRect(965, 450, 211, 20))
            font = QtGui.QFont()
            font.setFamily("맑은 고딕")
            self.SearchInput.setFont(font)
            self.SearchInput.setObjectName("SearchInput")
            self.SearchClick = QtWidgets.QPushButton(self.centralwidget)
            self.SearchClick.setGeometry(QtCore.QRect(1184, 450, 81, 23))
            font = QtGui.QFont()
            font.setFamily("맑은 고딕")
            self.SearchClick.setFont(font)
            self.SearchClick.setObjectName("SearchClick")
            self.SearchResult = QtWidgets.QTableWidget(self.centralwidget)
            self.SearchResult.setGeometry(QtCore.QRect(965, 480, 301, 192))
            self.SearchResult.setRowCount(0)
            self.SearchResult.setColumnCount(3)
            self.SearchResult.setObjectName("SearchResult")
            item = QtWidgets.QTableWidgetItem()
            self.SearchResult.setHorizontalHeaderItem(0, item)
            item = QtWidgets.QTableWidgetItem()
            self.SearchResult.setHorizontalHeaderItem(1, item)
            item = QtWidgets.QTableWidgetItem()
            self.SearchResult.setHorizontalHeaderItem(2, item)
            SquadMaker.setCentralWidget(self.centralwidget)
            self.statusbar = QtWidgets.QStatusBar(SquadMaker)
            self.statusbar.setObjectName("statusbar")
            SquadMaker.setStatusBar(self.statusbar)

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

       def retranslateUi(self, SquadMaker):
            _translate = QtCore.QCoreApplication.translate
            SquadMaker.setWindowTitle(_translate("SquadMaker", "MainWindow"))
            self.SearchClick.setText(_translate("SquadMaker", "Search"))
            item = self.SearchResult.horizontalHeaderItem(0)
            item.setText(_translate("SquadMaker", "Nationality"))
            item = self.SearchResult.horizontalHeaderItem(1)
            item.setText(_translate("SquadMaker", "Player Name"))
            item = self.SearchResult.horizontalHeaderItem(2)
            item.setText(_translate("SquadMaker", "Salary"))


if __name__ == "__main__":
    import sys
    app = QtWidgets.QApplication(sys.argv)
    SquadMaker = QtWidgets.QMainWindow()
    ui = Ui_SquadMaker()
    ui.setupUi(SquadMaker)
    SquadMaker.show()
    sys.exit(app.exec_())

0 个答案:

没有答案