def refreshButtonClicked(self): print('233') DB = SQLITE() content = DB.returnTableContent() del DB self.model.removeRows(0, self.model.rowCount()) for i in range(0, len(content)): for j in range(0, len(content[0])): self.model.setItem(i, j, QStandardItem(str(content[i][j])))
def __init__(self, parent=None): super(Main, self).__init__(parent) self.setWindowTitle("基于PyQt和SQLite的嵌入式课程设计") self.resize(500, 300) self.layout = QVBoxLayout() self.Hlayout = QHBoxLayout() self.insertButton = QPushButton("新增") self.alterButton = QPushButton("修改") self.deleteButton = QPushButton("删除") self.refreshButton = QPushButton("刷新") self.insertButton.clicked.connect(self.insertButtonClicked) self.alterButton.clicked.connect(self.alterButtonClicked) self.deleteButton.clicked.connect(self.deleteButtonClicked) self.refreshButton.clicked.connect(self.refreshButtonClicked) self.model = QStandardItemModel(7, 4) self.model.setHorizontalHeaderLabels( ['ID', 'First Name', 'Last Name', 'Phone Number']) # #Todo 优化2 添加数据 # self.model.appendRow([ # QStandardItem('row %s,column %s' % (11,11)), # QStandardItem('row %s,column %s' % (11,11)), # QStandardItem('row %s,column %s' % (11,11)), # QStandardItem('row %s,column %s' % (11,11)), # ]) DB = SQLITE() content = DB.returnTableContent() del DB for i in range(0, len(content)): for j in range(0, len(content[0])): self.model.setItem(i, j, QStandardItem(str(content[i][j]))) # 实例化表格视图,设置模型为自定义的模型 self.tableView = QTableView() self.tableView.setModel(self.model) Hlayout = QHBoxLayout() Hlayout.addWidget(self.insertButton) Hlayout.addWidget(self.alterButton) Hlayout.addWidget(self.deleteButton) Hlayout.addWidget(self.refreshButton) widget = QWidget() widget.setLayout(Hlayout) #widget.setFixedWidth(500) self.Hlayout.addWidget(widget) self.layout.addLayout(self.Hlayout) self.layout.addWidget(self.tableView) self.setLayout(self.layout)
def insertInfoButtonClicked(self): firstName = self.firstNameEdit.text() lastName = self.lastNameEdit.text() number = self.numberEdit.text() DB = SQLITE() DB.ExecNonQuery( f"INSERT INTO 'Users' (ID, FirstName, LastName, Phone)VALUES (NULL, '{firstName}', '{lastName}', '{number}')" ) del DB print( QMessageBox.warning(self, "提示", "添加成功!", QMessageBox.Yes, QMessageBox.Ok)) self.clearEdit() return
def idEditChanged(self): ID = self.idEdit.text() if (ID == ""): self.firstNameEdit.clear() self.lastNameEdit.clear() self.numberEdit.clear() # 查询对应ID,如果存在就更新form else: DB = SQLITE() self.result = DB.ExecQuery(f"SELECT * FROM Users WHERE ID={ID}") del DB if not self.result == []: self.firstNameEdit.setText(self.result[0][1]) self.lastNameEdit.setText(self.result[0][2]) self.numberEdit.setText(self.result[0][3]) return
def alterInfoButtonClicked(self): ID = self.idEdit.text() firstName = self.firstNameEdit.text() lastName = self.lastNameEdit.text() number = self.numberEdit.text() DB = SQLITE() result = DB.ExecQuery(f"SELECT * FROM Users WHERE ID = {ID}") del DB if result == []: print( QMessageBox.warning(self, "警告", "该用户不存在,请检查ID输入!", QMessageBox.Yes, QMessageBox.Ok)) self.clearEdit() return else: DB = SQLITE() DB.ExecNonQuery( f"UPDATE Users SET FirstName='{firstName}',LastName='{lastName}',Phone='{number}' WHERE ID = {ID}" ) del DB print( QMessageBox.warning(self, "提示", "修改成功!", QMessageBox.Yes, QMessageBox.Ok)) self.clearEdit() return
def deleteInfoButtonClicked(self): ID = self.idEdit.text() firstName = self.firstNameEdit.text() lastName = self.lastNameEdit.text() number = self.numberEdit.text() DB = SQLITE() result = DB.ExecQuery(f"SELECT * FROM Users WHERE ID = {ID}") del DB if result == []: print( QMessageBox.warning(self, "警告", "该用户不存在,请检查ID输入!", QMessageBox.Yes, QMessageBox.Ok)) self.clearEdit() return else: DB = SQLITE() DB.ExecNonQuery(f"DELETE FROM Users WHERE ID = {ID}") del DB print( QMessageBox.warning(self, "提示", "删除成功!", QMessageBox.Yes, QMessageBox.Ok)) self.clearEdit() return