当数据只是文本时,我有一个编辑数据的系统。我添加了一个组合框,我希望其中的数据也可以编辑。更改组合框并单击编辑按钮后,我希望数据在数据库中更新。我怎么做? 这是我输出的一个查询的代码。
def click_btn_lessons(self):
self.screen_name = "lessons"
self.page_constants()
self.cur.execute("SELECT StudentID FROM StudentProfile")
students_students = self.cur.fetchall()
self.cur.execute("SELECT StudentID FROM Lessons")
students_lessons = self.cur.fetchall()
self.cur.execute("""SELECT s.studentID, s.FullName, l.LessonDate, r.RoadExerciseName, e.Rating
FROM StudentProfile s
LEFT JOIN Lessons l ON s.StudentID=l.StudentID
LEFT JOIN LessonExercises e ON l.LessonID=e.LessonID
LEFT JOIN RoadExerciseInfo r ON r.RoadExerciseID=e.RoadExerciseID
""")
self.all_data = self.cur.fetchall()
"""This section just removes black records"""
for student in students_students:
if student not in students_lessons:
for item in self.all_data:
if item[0] == student[0]:
self.all_data.remove(item)
self.table.setRowCount(len(self.all_data))
self.tableFields = ['check','Full name','Lesson date', 'Exercise', "Rating"]
self.table.setColumnCount(len(self.tableFields))
self.table.setHorizontalHeaderLabels(self.tableFields)
self.checkbox_list = []
for i, item in enumerate(self.all_data):
FullName = QtGui.QTableWidgetItem(str(item[1]))
LessonDate = QtGui.QTableWidgetItem(str(item[2]))
RoadExerciseName = QtGui.QTableWidgetItem(str(item[3]))
#This is the combobox and its items
Rating = QtGui.QComboBox()
for num in range(5):
Rating.addItem(str(num))
self.table.setItem(i, 1, FullName)
self.table.setItem(i, 2, LessonDate)
self.table.setItem(i, 3, RoadExerciseName)
#here is how the combobox isadded to the table
self.table.setCellWidget(i, 4, Rating)
#the checkbox items have to be ticked for them to be passed to the edit function
chkBoxItem = QtGui.QTableWidgetItem()
chkBoxItem.setFlags(QtCore.Qt.ItemIsUserCheckable | QtCore.Qt.ItemIsEnabled)
chkBoxItem.setCheckState(QtCore.Qt.Unchecked)
self.checkbox_list.append(chkBoxItem)
self.table.setItem(i, 0, self.checkbox_list[i])
FullName.setFlags(FullName.flags() & ~Qt.ItemIsEditable)
RoadExerciseName.setFlags(RoadExerciseName.flags() & ~Qt.ItemIsEditable)
self.name = None
self.changed_items = []
self.table.itemChanged.connect(self.log_change)
def log_change(self, item):
self.table.blockSignals(True)
item.setBackgroundColor(QtGui.QColor("red"))
self.table.blockSignals(False)
self.changed_items.append(item)
def click_btn_edit(self):
print("Updating")
for item in self.changed_items:
self.table.blockSignals(True)
item.setBackgroundColor(QtGui.QColor("white"))
self.table.blockSignals(False)
text, col, row = item.text(), item.column(), item.row()
new_data = self.all_data
for i, record in enumerate(self.all_data):
if i == row:
if self.checkbox_list[i].checkState() == QtCore.Qt.Checked:
#Below adds the new data to the database.
self.cur.execute("""UPDATE {0} SET {1} = ? WHERE {2} = ?""".format(self.table_edit_name,self.columnList[col],self.field_ID),(text,record[0],))
"""self.field_ID is the field name for the primary key, record[0]. This ensures data is entered in the correct record.
columnList is a list of columns in the record, in order.
self.table_edit_name is the name of the table involved."""