我在代码中有一个从sqlite数据库(ref LaBase)中提取的简单TableView,主要有2列填充datetime.I在datetime1的每一行datetime2上减去时间以获得时间增量。问题是 : 1 /如何显示结果,即第3列中的时间增量和 2 /如何将结果插入数据库? 相关代码:
class MainDialog(QDialog, TabView.Ui_Dialog):
def __init__(self, parent=None):
super(MainDialog, self).__init__(parent)
self.setupUi(self)
# appel de la classe du module dbessai
self.LaBase = LaBase()
self.model = QSqlTableModel()
self.model.setTable("Contact")
self.model.setEditStrategy(QSqlTableModel.OnRowChange)
self.model.select()
self.model.setHeaderData(0, Qt.Horizontal, "ID")
self.model.setHeaderData(1, Qt.Horizontal, "pilot_1")
self.model.setHeaderData(2, Qt.Horizontal, "datetime1")
self.model.setHeaderData(3, Qt.Horizontal, "datetime2")
self.model.setHeaderData(4, Qt.Horizontal, "Calcul")
self.tableView.setModel(self.model)
def setdata(self):
query = QSqlQuery()
query.prepare("INSERT INTO Contact (pilot_1,datetime1,datetime2,total)" "VALUES (?,?,?,?)")
query.bindValue(0, self.lineEdit_pilote.text())
query.bindValue(1, self.dateTimeEdit.text())
query.bindValue(2, self.dateTimeEdit_2.text())
query.bindValue(3, self.get_date_diff()) #This line is wrong
query.exec_()
self.model.select()
def remove_row(self):
index = self.tableView.currentIndex()
deleteconf = QMessageBox.critical(self.parent(), "DELETE ROW", "REALLY DELETE", QMessageBox.Yes,
QMessageBox.No)
if deleteconf == QMessageBox.Yes:
self.model.removeRow(index.row())
self.model.submitAll()
self.model.select()
return
else:
return
def get_date_diff(self):
quer = QSqlTableModel(self.model)
quer.setTable("Contact")
quer.select()
i = 0
while i < quer.rowCount():
time1 = quer.record(i).value("datetime1")
time2 = quer.record(i).value("datetime2")
i += 1
diff = datetime.strptime(time2, "%d/%m/%Y %H:%M") - datetime.strptime(time1, "%d/%m/%Y %H:%M")
return diff
@pyqtSlot()
def on_calculer_clicked(self):
self.get_date_diff()
@pyqtSlot()
def on_pushButton_4_clicked(self):
self.setdata()
@pyqtSlot()
def on_Effacer_clicked(self):
self.remove_row()
只需将query.bindValue(3, self.get_date_diff())
转换为query.bindValue(3, str(self.get_date_diff()))
我就能得到我想要的东西,但我只在第一行得到它...我想我需要在方法remove_row中的某个地方使用for循环来获取所有内容线条。有什么想法吗?