PyQt5: chapter9-在指定数据库表的行中导航

实例

  1. 创建基于Dialog without Buttons模板窗口
  2. 添加三个Label,两个LineEdit,四个PushButton部件
  3. 设定Label的text为Email Address,Password
  4. 设定四个PushButton的text为First Row, Previous, Next, Last Row
  5. 设定Line Edit的objectName为lineEditEmailAddress ,lineEditPassword
  6. 设定PushButton的objectName为pushButtonFirst, pushButtonPrevious, pushButtonNext, pushButtonLast
  7. 设定第三个Label的object Name为labelResponse
  8. 选择Line Edit(password)的echo Mode为Password,替代默认的Normal
  9. 保存为demoShowRecords.ui
  10. 使用pyuic生成demoShowRecords.py
  11. 创建callShowRecords.py,代码如下
     
import sqlite3, sys
from PyQt5.QtWidgets import QDialog, QApplication,QTableWidgetItem
from sqlite3 import Error
from cookbook_200503.demoShowRecords import *
rowNo=1
sqlStatement="SELECT EmailAddress, Password FROM Users"
conn = sqlite3.connect("ECommerce.db")
cur = conn.cursor()
class MyForm(QDialog):
    def __init__(self):
        super().__init__()
        self.ui = Ui_Dialog()
        self.ui.setupUi(self)
        cur.execute(sqlStatement)
        self.ui.pushButtonFirst.clicked.connect(self.ShowFirstRow)
        self.ui.pushButtonPrevious.clicked.connect(self.ShowPreviousRow)
        self.ui.pushButtonNext.clicked.connect(self.ShowNextRow)
        self.ui.pushButtonLast.clicked.connect(self.ShowLastRow)
        self.show()
    def ShowFirstRow(self):
        try:
            cur.execute(sqlStatement)
            row = cur.fetchone()
            if row:
                self.ui.lineEditEmailAddress.setText(row[0])
                self.ui.lineEditPassword.setText(row[1])
        except Error as e:
            self.ui.labelResponse.setText("Error in accessing table")
    def ShowPreviousRow(self):
        global rowNo
        rowNo -= 1
        sqlStatement = "SELECT EmailAddress, Password FROM Users where rowid=" + str(rowNo)
        cur.execute(sqlStatement)
        row = cur.fetchone()
        if row:
            self.ui.labelResponse.setText("")
            self.ui.lineEditEmailAddress.setText(row[0])
            self.ui.lineEditPassword.setText(row[1])
        else:
            rowNo += 1
            self.ui.labelResponse.setText("This is the first row")
    def ShowNextRow(self):
        global rowNo
        rowNo += 1
        sqlStatement = "SELECT EmailAddress, Password FROM Users where rowid=" + str(rowNo)
        cur.execute(sqlStatement)
        row = cur.fetchone()
        if row:
            self.ui.labelResponse.setText("")
            self.ui.lineEditEmailAddress.setText(row[0])
            self.ui.lineEditPassword.setText(row[1])
        else:
            rowNo -= 1
            self.ui.labelResponse.setText("This is the last row")
    def ShowLastRow(self):
        cur.execute(sqlStatement)
        for row in cur.fetchall():
            self.ui.lineEditEmailAddress.setText(row[0])
            self.ui.lineEditPassword.setText(row[1])
if __name__ == "__main__":
    app = QApplication(sys.argv)
    w = MyForm()
    w.show()
    sys.exit(app.exec())

猜你喜欢

转载自blog.csdn.net/weixin_43307431/article/details/105906149
今日推荐