如何在Qt设计器生成的python文件中从数据库添加表

时间:2019-11-19 19:22:13

标签: python-3.x sqlite pyqt5

我正在尝试创建一个Python GUI程序。 GUI是使用Qt设计器创建的。该目标程序将搜索数据库并以表格式显示文件。但是由于某些问题,程序无法显示该表。

完整程序如下

from PyQt5 import QtCore, QtGui, QtWidgets
import sqlite3
import os
import datetime

os.chdir('F:\\Gk_Data')

conn = sqlite3.connect('gktoday.db')
c = conn.cursor()


def dateCon(x):
    y = datetime.datetime.strptime(str(x), "%d-%m-%Y").strftime("%Y-%m-%d")
    z = y.replace("-", "")
    return z


class Ui_MainWindow(object):
    def setupUi(self, MainWindow):
        MainWindow.setObjectName("MainWindow")
        MainWindow.resize(729, 494)
        self.centralwidget = QtWidgets.QWidget(MainWindow)
        self.centralwidget.setObjectName("centralwidget")
        self.frame = QtWidgets.QFrame(self.centralwidget)
        self.frame.setGeometry(QtCore.QRect(30, 50, 661, 51))
        self.frame.setFrameShape(QtWidgets.QFrame.StyledPanel)
        self.frame.setFrameShadow(QtWidgets.QFrame.Raised)
        self.frame.setObjectName("frame")

        # search input
        self.search_input = QtWidgets.QLineEdit(self.frame)
        self.search_input.setGeometry(QtCore.QRect(30, 10, 521, 31))
        self.search_input.setObjectName("search_input")

        # search btn
        self.search_btn = QtWidgets.QPushButton(self.frame)
        self.search_btn.setGeometry(QtCore.QRect(570, 10, 81, 31))
        self.search_btn.setObjectName("search_btn")

        self.frame_2 = QtWidgets.QFrame(self.centralwidget)
        self.frame_2.setGeometry(QtCore.QRect(30, 20, 661, 31))
        self.frame_2.setFrameShape(QtWidgets.QFrame.StyledPanel)
        self.frame_2.setFrameShadow(QtWidgets.QFrame.Raised)
        self.frame_2.setObjectName("frame_2")
        self.layoutWidget = QtWidgets.QWidget(self.frame_2)
        self.layoutWidget.setGeometry(QtCore.QRect(40, 10, 227, 22))
        self.layoutWidget.setObjectName("layoutWidget")
        self.horizontalLayout = QtWidgets.QHBoxLayout(self.layoutWidget)
        self.horizontalLayout.setContentsMargins(0, 0, 0, 0)
        self.horizontalLayout.setObjectName("horizontalLayout")
        self.label = QtWidgets.QLabel(self.layoutWidget)
        self.label.setObjectName("label")
        self.horizontalLayout.addWidget(self.label)

        # date select from
        self.from_select = QtWidgets.QDateEdit(self.layoutWidget)
        self.from_select.setDateTime(QtCore.QDateTime(QtCore.QDate(2012, 1, 1), QtCore.QTime(0, 0, 0)))
        self.from_select.setObjectName("from_select")
        self.horizontalLayout.addWidget(self.from_select)

        self.label_2 = QtWidgets.QLabel(self.layoutWidget)
        self.label_2.setObjectName("label_2")
        self.horizontalLayout.addWidget(self.label_2)

        # date select to
        self.to_select = QtWidgets.QDateEdit(self.layoutWidget)
        self.to_select.setDateTime(QtCore.QDateTime(QtCore.QDate(2013, 1, 1), QtCore.QTime(0, 0, 0)))
        self.to_select.setObjectName("to_select")
        self.horizontalLayout.addWidget(self.to_select)

        # table display
        self.table_print = QtWidgets.QTableView(self.centralwidget)
        self.table_print.setGeometry(QtCore.QRect(30, 120, 661, 351))
        self.table_print.setObjectName("table_print")

        self.layoutWidget1 = QtWidgets.QWidget(self.centralwidget)
        self.layoutWidget1.setGeometry(QtCore.QRect(0, 0, 2, 2))
        self.layoutWidget1.setObjectName("layoutWidget1")
        self.verticalLayout_2 = QtWidgets.QVBoxLayout(self.layoutWidget1)
        self.verticalLayout_2.setContentsMargins(0, 0, 0, 0)
        self.verticalLayout_2.setObjectName("verticalLayout_2")
        MainWindow.setCentralWidget(self.centralwidget)

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

        # add code here
        self.search_btn.clicked.connect(self.printMsg)

    def retranslateUi(self, MainWindow):
        _translate = QtCore.QCoreApplication.translate
        MainWindow.setWindowTitle(_translate("MainWindow", "MainWindow"))
        self.search_btn.setText(_translate("MainWindow", "Search"))
        self.label.setText(_translate("MainWindow", "Duration"))
        self.from_select.setDisplayFormat(_translate("MainWindow", "dd-MM-yyyy"))
        self.label_2.setText(_translate("MainWindow", "-"))

    def printMsg(self):
        termvalue = self.search_input.text()
        fvalue  = self.from_select.text()
        tvalue = self.to_select.text()

        beg = dateCon(fvalue)
        end = dateCon(tvalue)
        print(str(termvalue) + "  " + str(beg) + "  " + str(end))

        c.execute("SELECT Date, title FROM GkData WHERE "
                  "(Id BETWEEN '{}' AND '{}') AND (content LIKE '%{}%') ORDER BY Id DESC".format(beg, end, termvalue))
        data = c.fetchall()
        print(data)
        print(len(data))

        self.table_print.setRowCount(int(len(data)))

        self.table_print.setColumnCount(2)
        header_labels = ['Date', 'Title']
        self.table_print.setHorizontalHeaderLabels(header_labels)
        i = 0
        for j in data:
            self.table_print.setItem(i, 0, QtWidgets.QTableWidgetItem(j[0]))
            self.table_print.setItem(i, 1, QtWidgets.QTableWidgetItem(j[1]))
            i = i + 1



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


c.close
conn.close()

我认为问题是当我尝试显示表格时。由于代码程序中的某些错误而崩溃。我是PyQt5和编程的新手。请帮助。

使用搜索词“新”获取的2012年至2013年期间的数据如下所示

[('December 13, 2012', 'WHO instructs India to keep lens on tourist for SARS-like virus'), ('December 10, 2012', 'India ranked 4th on Global Terrorism Index')]

该程序的屏幕截图如下

GUI

0 个答案:

没有答案