实例
- 创建基于Dialog without Buttons模板窗口
- 添加五个Label,四个LineEdit,一个PushButton部件
- 设定四个Label的text为Enter database name, Enter table name, Email Address, Password
- 设定PushButton的text为Insert Row
- 设定四个Line Edit的object Name为lineEditDBName, lineEditTableName, lineEditEmailAddress,lineEditPassword
- 设定PushButton的objectName为pushButtonInsertRow
- 设定第五个Label的objectName为labelResponse
- 选择Line Edit(password)的echo Mode为Password,替代默认的Normal
- 保存为demoInsertRowsInTable.ui
- 使用pyuic生成demoInsertRowsInTable.py
- 创建callInsertRows.py,代码如下
import sys,sqlite3
from PyQt5.QtWidgets import QDialog,QApplication
from sqlite3 import Error
from cookbook_200503.demoInsertRowsInTable import *
class MyForm(QDialog):
def __init__(self):
super().__init__()
self.ui=Ui_Dialog()
self.ui.setupUi(self)
self.ui.pushButtonInsertRow.clicked.connect(self.InsertRows)
self.show()
def InsertRows(self):
sqlStatement="INSERT INTO "+self.ui.lineEditTableName.text()+" VALUES('"+self.ui.lineEditEmailAddress.text()+"','"+self.ui.lineEditPassword.text()+"')"
try:
conn=sqlite3.connect(self.ui.lineEditDBName.text()+".db")
with conn:
cur=conn.cursor()
cur.execute(sqlStatement)
self.ui.labelResponse.setText("Row successfully inserted")
except Error as e:
self.ui.labelResponse.setText("Error in inserting row")
finally:
conn.close()
if __name__=="__main__":
app=QApplication(sys.argv)
w=MyForm()
w.show()
sys.exit(app.exec())
![](https://img-blog.csdnimg.cn/2020050317284044.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3dlaXhpbl80MzMwNzQzMQ==,size_16,color_FFFFFF,t_70)