QSqlRecord不更新数据库

时间:2018-11-19 21:04:55

标签: python python-3.x pyqt pyqt5

我有以下似乎无效的代码:

model = QSqlTableModel()
model.setTable("clidata")
model.setFilter("recordid=1")
model.select() #Only one record is loaded

record = model.record()
for ndx in range(0, record.count()):
    record.setGenerated(ndx, False)
record.setValue("recordid", 1)
record.setValue("companyname", "TEST")
record.setValue("address", "ADDRESS")
record.setValue("taxid", "TAX ID")
record.setValue("active", 2)

model.setRecord(0,record)
model.submitAll()
print(model.lastError().text())

此代码的输出表明没有要更新的字段;即使所有字段(recordid除外)都已更改。我错过了一步吗?

1 个答案:

答案 0 :(得分:0)

显然,我将QSqlRecord的setGenerated()方法读错了,因为将False更改为True会正确更新数据库。