先展示先PyQt5连接MySql的效果

PyQt5连接MySQL-LMLPHP

1 ,布局


2 ,与MySql建立连接

PyQt5连接MySQL-LMLPHP

3 ,信号和槽建立连接

PyQt5连接MySQL-LMLPHP

4,查询按钮的内容

 def buttonTest(self):
        temp_sqlstring = self.sqlstring
        is_first = True
        if self.check_Sid.isChecked():
            mystr = self.Sid.text()
            if is_first:
                is_first = False
                if mystr.find("%") == -1:
                    temp_sqlstring += "Sid = '" + self.Sid.text() + "'"
                else:
                    temp_sqlstring += "Sid like '" + self.Sid.text() + "'"
            else:
                if mystr.find("%") == -1:
                    temp_sqlstring += " and Sid = '" + self.Sid.text() + "'"
                else:
                    temp_sqlstring += " and Sid like '" + self.Sid.text() + "'"

        if self.check_Sname.isChecked():
            if is_first:
                mystr = self.Sname.text()
                is_first = False
                if mystr.find("%") == -1:
                    temp_sqlstring += "Sname = '" + self.Sname.text() + "'"
                else:
                    temp_sqlstring += "Sname like '" + self.Sname.text() + "'"
            else:
                if mystr.find("%") == -1:
                    temp_sqlstring += " and Sname = '" + self.Sname.text() + "'"
                else:
                    temp_sqlstring += " and Sname like '" + self.Sname.text() + "'"

        if self.check_Sage.isChecked():
            if is_first:
                is_first = False
                temp_sqlstring += "Sage >= " + self.first_Sage.text() + \
                                  " and Sage <= " + self.last_Sage.text()
            else:
                temp_sqlstring += " and Sage >= " + self.first_Sage.text() + \
                                  " and Sage <= " + self.last_Sage.text()

        if self.check_Ssex.isChecked():
            if is_first:
                is_first = False
                temp_sqlstring += "Ssex = '" + self.Ssex.text() + "'"
            else:
                temp_sqlstring += " and Ssex = '" + self.Ssex.text() + "'"

        if self.check_Sclass.isChecked():
            if is_first:
                mystr = self.Sclass.text()
                is_first = False
                if mystr.find("%") == -1:
                    temp_sqlstring += "Sclass = '" + self.Sclass.text() + "'"
                else:
                    temp_sqlstring += "Sclass like '" + self.Sclass.text() + "'"
            else:
                if mystr.find("%") == -1:
                    temp_sqlstring += " and Sclass = '" + self.Sclass.text() + "'"
                else:
                    temp_sqlstring += " and Sclass like '" + self.Sclass.text() + "'"

        if self.check_Sdept.isChecked():
            if is_first:
                mystr = self.Sdept.text()
                is_first = False
                if mystr.find("%") == -1:
                    temp_sqlstring += "Sdept = '" + self.Sdept.text() + "'"
                else:
                    temp_sqlstring += "Sdept like '" + self.Sdept.text() + "'"
            else:
                if mystr.find("%") == -1:
                    temp_sqlstring += " and Sdept = '" + self.Sdept.text() + "'"
                else:
                    temp_sqlstring += " and Sdept like '" + self.Sdept.text() + "'"

        if self.check_Saddr.isChecked():
            if is_first:
                mystr = self.Saddr.text()
                is_first = False
                if mystr.find("%") == -1:
                    temp_sqlstring += "Saddr = '" + self.Saddr.text() + "'"
                else:
                    temp_sqlstring += " and Saddr like '" + self.Saddr.text() + "'"
            else:
                if mystr.find("%") == -1:
                    temp_sqlstring += " and Saddr = '" + self.Saddr.text() + "'"
                else:
                    temp_sqlstring += " and Saddr like '" + self.Saddr.text() + "'"

        self.result_out.clearContents()  # 每一次查询时清除表格中信息
        if not (is_first):
            print(temp_sqlstring)
            self.cur.execute(temp_sqlstring)
            k = 0
            for i in self.cur:
                print("----------",i)
                w = 0
                for j in i:
                    # 这里是将int类型转成string类型,方便后面文本设置
                    if type(j) == int:
                        newItem =  QTableWidgetItem(str(j))

                    else:
                        newItem =  QTableWidgetItem(j)
                    # 根据循环标签一次对table中的格子进行设置
                    self.result_out.setItem(k, w, newItem)
                    w += 1
                k += 1

        self.sql_out.setText("")
        self.sql_out.append(temp_sqlstring)
        print("find button pressed")

5,往表格里面填数据

PyQt5连接MySQL-LMLPHP

github源码地址

https://github.com/jeekMic/py_mysql.git

--------------------- 本文来自 jeekmary 的CSDN 博客 ,全文地址请点击:https://blog.csdn.net/jeekmary/article/details/79677285?utm_source=copy

10-04 11:17