PYQT:QTableView。在日期之间过滤

PYQT: QTableView. Filter Between Dates

我创建了一个 GUI,允许用户修改、过滤更新和从 sqlite 数据库中删除。除了日期范围过滤器外,一切都很好。当我 运行 代码时,我没有得到错误,我只得到一个没有数据的空白过滤屏幕。有人看到我的代码有什么问题吗? [Section 1a, subgroup{v} 是我正在寻求帮助的内容]。谢谢!

from PyQt4 import QtCore, QtGui, QtSql
import sys  
import sqlite3
import time
import Search  #imported ui.py MainWindow file
import os 
try:
    from PyQt4.QtCore import QString
except ImportError:
    QString = str

class TableEditor(QtGui.QMainWindow, Search.Search_MainWindow):
    def __init__(self, tableName, parent=None):
        super(self.__class__, self).__init__()
        self.setupUi(self)

        self.model = QtSql.QSqlTableModel(self)
        self.model.setTable('CAUTI')
        self.model.setEditStrategy(QtSql.QSqlTableModel.OnManualSubmit)
        self.model.select()

        self.model.setHeaderData(0, QtCore.Qt.Horizontal, "MRN")
        self.model.setHeaderData(1, QtCore.Qt.Horizontal, "Last Name")
        self.model.setHeaderData(2, QtCore.Qt.Horizontal, "First Name")
        self.model.setHeaderData(3, QtCore.Qt.Horizontal, "Date of Event")
        self.model.setHeaderData(4, QtCore.Qt.Horizontal, "Facility")
        self.model.setHeaderData(5, QtCore.Qt.Horizontal, "Unit")
        self.model.setHeaderData(6, QtCore.Qt.Horizontal, "User")
        self.tableView.setModel(self.model)
        self.setWindowTitle("HAI Table")
        self.tableView.setColumnWidth(0,100)
        self.tableView.setColumnWidth(1,100)
        self.tableView.setColumnWidth(2,100)
        self.tableView.setColumnWidth(3,100)
        self.tableView.setColumnWidth(4,100)
        self.tableView.setColumnWidth(5,100)
        self.tableView.setColumnWidth(6,83)
        self.submitButton.clicked.connect(self.submit)
        self.revertButton.clicked.connect(self.model.revertAll)
        self.quitButton.clicked.connect(self.close)

        current = QtCore.QDateTime.currentDateTime()
        self.startDate.setDate(current.date())
        self.endDate.setDate(current.date())
        self.startDate.setDisplayFormat("M/dd/yyyy")
        self.endDate.setDisplayFormat("M/dd/yyyy")


# Section 1: Signals 

    # {i} Search Fields Button Emitted:
    # [1]
        self.search_MRN_Button.clicked.connect(self.search_MRN_FilterRecord)
    # [2]
        self.search_Lname_Button.clicked.connect(self.search_Lname_FilterRecord)
    # [3]
        self.search_Unit_Button.clicked.connect(self.search_Unit_FilterRecord)

    # {ii} Search Clear Buttons Emitted: 
    # [1]
        self.search_MRN_CancelButton.clicked.connect(self.search_MRN_CancelButton_Clicked)
    # [2]
        self.search_Lname_CancelButton.clicked.connect(self.search_Lname_CancelButton_Clicked)
    # [3]
        self.search_Unit_CancelButton.clicked.connect(self.search_Unit_CancelButton_Clicked)

    # {iii} Search Fields Button Emitted:
    # [1]
        self.search_MRN.selectionChanged.connect(self.search_MRN_Edit)
    # [2]
        self.search_Lname.selectionChanged.connect(self.search_Lname_Edit)
    # [3]
        self.search_Unit.selectionChanged.connect(self.search_Unit_Edit)
    # {iv} Search Fields Button Emitted:
    # [1]
        self.search_MRN.returnPressed.connect(self.search_MRN_Enter)
    # [2]
        self.search_Lname.returnPressed.connect(self.search_Lname_Enter)
    # [3]
        self.search_Unit.returnPressed.connect(self.search_Unit_Enter)

    #{v} Search Between 2 Dates
        self.btnSubmit.clicked.connect(self.FilterBetweenDates)

# Section 1a: Slots from Section 1. 

    #{i} Search Field Button Slots:
    #[1]
    def search_MRN_FilterRecord(self):
        text = self.search_MRN.text()
        if len(text) == 0:
            self.model.setFilter("")
        else:
            self.model.setFilter("MRN like'" +self.search_MRN.text()+ "%%'")

            #self.model.setFilter("MRN = '%s'" % text)
    #[2]
    def search_Lname_FilterRecord(self):
        text = self.search_Lname.text()
        if len(text) == 0:
            self.model.setFilter("")
        else:
            self.model.setFilter("Surname like'" +self.search_Lname.text()+ "%'")
            #self.model.setFilter("Surname = '%s'" % text) #This line of code will only pull exact matches.
    #[3]
    def search_Unit_FilterRecord(self):
        text = self.search_Unit.text()
        if len(text) == 0:
            self.model.setFilter("")
        else:
            self.model.setFilter("Unit like'" +self.search_Unit.text()+ "%'")
            #self.model.setFilter("Unit = '%s'" % text) #This line of code will only pull exact matches.

    #{ii} Search Field Cancel Button Slots:
    #[1]
    def search_MRN_CancelButton_Clicked(self):
        self.model.setFilter("")
        self.search_MRN.setText("MRN Search")
    #[2]
    def search_Lname_CancelButton_Clicked(self):
        self.model.setFilter("")
        self.search_Lname.setText("Last Name Search")
    #[3]
    def search_Unit_CancelButton_Clicked(self):
        self.model.setFilter("")
        self.search_Unit.setText("Unit Search")

    #{iii} Search Text Edited Slots:
    #[1]    
    def search_MRN_Edit(self):
        self.search_MRN.setText("")
    #[2]
    def search_Lname_Edit(self):
        self.search_Lname.setText("") 
    #[3]      
    def search_Unit_Edit(self):
        self.search_Unit.setText("")

    #{iv} Search Text Return Pressed (Enter) Slots:
    #[1]    
    def search_MRN_Enter(self):
        self.search_MRN_FilterRecord()
    #[2]
    def search_Lname_Enter(self):
        self.search_Lname_FilterRecord()
    #[3]      
    def search_Unit_Enter(self):
        self.search_Unit_FilterRecord()

    #{v} Filter Between Dates, Slot:
    def FilterBetweenDates(self):
        start = str(self.startDate.text())
        finish = str(self.endDate.text())
        self.model.setFilter("EventDate BETWEEN'" + start and finish)

    def submit(self):
        self.model.database().transaction()
        if self.model.submitAll():
            self.model.database().commit()
        else:
            self.model.database().rollback()
            QtGui.QMessageBox.warning(self, "HAI Table",
                        "The database reported an error: %s" % self.model.lastError().text())




def main():
    app = QtGui.QApplication(sys.argv)  
    #app.setStyle( "Plastique" )
    db = QtSql.QSqlDatabase.addDatabase('QSQLITE')
    db.setDatabaseName('HAI.db')

    editor = TableEditor('CAUTI')
    editor.show()
    app.exec_()  


if __name__ == '__main__': 

过滤器文本中的 and 应该是过滤器字符串的一部分。现在您正在获取两个字符串的 logical-and(除非第一个字符串为空,否则它将是最后一个字符串)并将其附加到过滤器字符串。

所以改变

self.model.setFilter("EventDate BETWEEN'" + start and finish)

filter = "EventDate BETWEEN '{}' AND '{}'".format(start, finish)
print(filter) # for debugging
self.model.setFilter(filter)

在调试过程中,打印过滤器字符串并在 SQL 查询中(直接在 sqlite3 工具中)尝试它可能是个好主意,这样您就可以看到它是否有效。