PyQt在sql查询中独占OR

时间:2014-08-26 22:38:40

标签: sql pyqt xor

如果我的第一次搜索显示结果,它不会执行查询的第二部分,但是停止并显示结果,我该怎么做?我试过这样的东西,但它只是给了我一个空白的窗口,而且它非常混乱:

def test_update(self):
          projectModel = QSqlQueryModel()
          projectModel.setQuery("""SELECT * FROM pacijent WHERE prezime = '%s' OR (prezime, 3) = metaphone('%s', 3) OR LEVENSHTEIN(LOWER(prezime), '%s') < 3 AND NOT (prezime = '%s' AND (prezime, 3) = metaphone('%s', 3) AND LEVENSHTEIN(LOWER(prezime), '%s') < 3)""" % (str(self.lineEdit.text()), str(self.lineEdit.text()), str(self.lineEdit.text()), str(self.lineEdit.text()), str(self.lineEdit.text()), str(self.lineEdit.text())))
          global projectView
          projectView = QtGui.QTableView()
          projectView.setModel(projectModel)
          projectView.show()

所以,如果它找到了属性&#34; prezime&#34;的确切值。它应该显示它,但如果它没有它应该要求更多的先进的saerch战术,如metaphone和levenshtein。

修改

我得到了这样的工作:

ef search_data(self):
              myQSqlQueryModel = QSqlQueryModel()
              query = QSqlQueryModel()
              global myQTableView 
              myQTableView = QtGui.QTableView()
              querySuccess = False
              for queryCommand in [""" SELECT * FROM "%s" WHERE "%s" = '%s' """ % (str(self.search_from_table_lineEdit.text()), str(self.search_where_lineEdit.text()), str(self.search_has_value_lineEdit.text()))]:
                     myQSqlQueryModel.setQuery(queryCommand)
                     if myQSqlQueryModel.rowCount() > 0:
                            myQTableView.setModel(myQSqlQueryModel)
                            myQTableView.show()
                            querySuccess = True
                            break
              if not querySuccess:
                     query.setQuery(""" SELECT * FROM "%s" WHERE METAPHONE("%s", 3) = METAPHONE('%s', 3) OR LEVENSHTEIN("%s", '%s') < 4 """ % (str(self.search_from_table_lineEdit.text()), str(self.search_where_lineEdit.text()), str(self.search_has_value_lineEdit.text()), str(self.search_where_lineEdit.text()), str(self.search_has_value_lineEdit.text())))
                     global var
                     var = QtGui.QTableView()
                     var.setModel(query)
                     var.show()

1 个答案:

答案 0 :(得分:1)

查询成功后,如果this method中有任何行数,您可以检查模型中的数据。而你的for循环可以得到很多查询;

def testUpdate (self):
    myQSqlQueryModel = QtSql.QSqlQueryModel()
    myQTableView     = QtGui.QTableView()
    querySuccess = False
    for queryCommand in ["YOUR QUERY 1", "YOUR QUERY 2"]:
        myQSqlQueryModel.setQuery(queryCommand)
        if myQSqlQueryModel.rowCount() > 0:
            myQTableView.setModel(myQSqlQueryModel)
            myQTableView.show()
            querySuccess = True
            break
    if not querySuccess:
        QtGui.QMessageBox.critical(self, 'Query error', 'Not found')