实例
- 创建基于Dialog without Buttons模板窗口
- 添加五个Label,三个LineEdit,一个ComboBox,两个PushButton部件
- 设定四个Label部件的text为 Enter database name, Enter table name, Column Name, Data Type
- 设定Push Button的text为Add Column ,Create Table
- 设定Line Edit的objectName为lineEditDBName, lineEditTableName, lineEditColumnName
- 设定ComboBox的objectName为ComboBoxDataType
- 设定PushButton的objectName为pushButtonAddColumn and pushButtonCreateTable
- 设定第五个Label的objectName为labelResponse
- 保存为demoCreateTable.ui
- 使用pyuic生成demoCreateTable.py
- 创建callCreateTable.py,代码如下
import sys,sqlite3
from PyQt5.QtWidgets import QDialog,QApplication
from sqlite3 import Error
from cookbook_200503.demoCreateTable import *
tabledefinition=""
class MyForm(QDialog):
def __init__(self):
super().__init__()
self.ui=Ui_Dialog()
self.ui.setupUi(self)
self.ui.pushButtonCreateTable.clicked.connect(self.createTable)
self.ui.pushButtonAddColumn.clicked.connect(self.addColumns)
self.show()
def addColumns(self):
global tabledefinition
if tabledefinition=="":
tabledefinition="CREATE TABLE IF NOT EXISTS "+self.ui.lineEditTableName.text()+" ("+self.ui.lineEditColumnName.text()+" "+self.ui.comboBoxDataType.itemText(self.ui.comboBoxDataType.currentIndex())
else:
tabledefinition += "," + self.ui.lineEditColumnName.text() + " " + self.ui.comboBoxDataType.itemText(self.ui.comboBoxDataType.currentIndex())
self.ui.lineEditColumnName.setText("")
self.ui.lineEditColumnName.setFocus()
def createTable(self):
global tabledefinition
try:
conn=sqlite3.connect(self.ui.lineEditDBName.text()+".db")
self.ui.labelResponse.setText("Database is connected")
c=conn.cursor()
tabledefinition+=");"
c.execute(tabledefinition)
self.ui.labelResponse.setText("Table is successfully created")
except Error as e:
self.ui.labelResponse.setText("Error in creating table")
finally:
conn.close()
if __name__=="__main__":
app=QApplication(sys.argv)
w=MyForm()
w.show()
sys.exit(app.exec())