PyQt5: chapter9-在数据库表中搜索特定信息

实例

  1. 创建基于Dialog without Buttons模板窗口
  2. 添加五个Label,四个LineEdit,一个PushButton部件
  3. 设定前三个Label的text为Enter database name, Enter table name, Email Address
  4. 设定第四个Label的text为空,第五个Label的text为Password
  5. 设定PushButton的text为Search
  6. 设定Line Edit的objectName为lineEditDBName, lineEditTableName, lineEditEmailAddress, lineEditPassword
  7. 设定PushButton的objectName为pushButtonSearch
  8. 设定第四个Label的object Name为labelResponse
  9. 保存为demoSearchRows.ui
  10. 使用pyuic生成demoSearchRows.py
  11. 创建callSearchRows.py,代码如下
import sys,sqlite3
from PyQt5.QtWidgets import QDialog,QApplication
from sqlite3 import Error
from cookbook_200504.demoSearchRows import *

class MyForm(QDialog):
    def __init__(self):
        super().__init__()
        self.ui=Ui_Dialog()
        self.ui.setupUi(self)
        self.ui.pushButtonSearch.clicked.connect(self.SearchRows)
        self.show()
    def SearchRows(self):
        sqlStatement="SELECT Password FROM "+self.ui.lineEditTableName.text()+"  where EmailAddress like'"+self.ui.lineEditEmailAddress.text()+"'"
        try:
            conn=sqlite3.connect(self.ui.lineEditDBName.text()+".db")
            cur=conn.cursor()
            cur.execute(sqlStatement)
            row=cur.fetchone()
            if row==None:
                self.ui.labelResponse.setText("Sorry, No User found with this email address")
                self.ui.lineEditPassword.setText("")
            else:
                self.ui.labelResponse.setText("Email Address Found, Password of this User is :")
                self.ui.lineEditPassword.setText(row[0])
        except Error as e:
            self.ui.labelResponse.setText("Error in accessing row")
        finally:
            conn.close()
if __name__=="__main__":
    app=QApplication(sys.argv)
    w=MyForm()
    w.show()
    sys.exit(app.exec())

猜你喜欢

转载自blog.csdn.net/weixin_43307431/article/details/105924696