我想从多个 QT 小部件构建记录并将其存储在数据库中。以下脚本运行时没有错误,但不存储记录。部分问题是我无法确定数据库中已有的最后一个 ID,但即使我手动设置 ID,也不会写入任何内容。我在网上看到过使用 insertRecord 的示例,但 QT 文档建议使用 insertRow。请随时纠正我的方法。我是 Python 和 Qt 新手。
import sys
from PySide.QtCore import *
from PySide.QtGui import *
from PySide.QtSql import *
class MainWindow(QMainWindow):
def __init__(self, parent=None):
super(MainWindow, self).__init__(parent)
#Make Database
self.db = QSqlDatabase.addDatabase('QSQLITE')
self.db.setDatabaseName('C:/Users/dle/Documents/example1.sqlite')
self.db.open()
self.db.transaction()
self.db.exec_(
'CREATE TABLE t1'
'(id INTEGER PRIMARY KEY, f1 INTEGER NOT NULL, f2 INTEGER NOT NULL, f3 TEXT NOT NULL)'
)
self.db.exec_("INSERT INTO t1 VALUES(1, 10, 20, 'db works fine')")
self.db.commit()
#Create User Interface
self.f1 = QLineEdit()
self.f2 = QLineEdit()
self.f3 = QLineEdit()
self.storeButton = QPushButton("Store")
self.storeButton.clicked.connect(self.doStore)
vlayout = QVBoxLayout()
vlayout.addWidget(self.f1)
vlayout.addWidget(self.f2)
vlayout.addWidget(self.f3)
vlayout.addWidget(self.storeButton)
widget = QWidget()
widget.setLayout(vlayout)
self.setCentralWidget(widget)
def doStore(self):
self.dbModel = QSqlTableModel(self)
self.dbModel.setTable('t1')
ID = self.dbModel.query().lastInsertId() #this returns "None" even though last ID
#in table is 1
thisRecord = QSqlRecord
thisRecord = self.dbModel.record()
thisRecord.setValue(0, ID) # Does not write, even if ID is integer
print ID
thisRecord.setValue(1, int(self.f1.text()))
print int(self.f1.text())
thisRecord.setValue(2, int(self.f2.text()))
print int(self.f2.text())
thisRecord.setValue(3, self.f3.text())
print self.f3.text()
print thisRecord
self.dbModel.insertRecord(ID, thisRecord) # Does not write, even if ID is integer
# Doesn't record field 0 already have ID?
if __name__ == '__main__':
app = QApplication(sys.argv)
mainwindow = MainWindow()
mainwindow.show()
sys.exit(app.exec_())
最佳答案
如果您想在表末尾插入一行,则无需指定索引,因为根据docs :
PySide.QtSql.QSqlTableModel.insertRecord(row, record)
Parameters:
row - PySide.QtCore.int record – PySide.QtSql.QSqlRecord
Return type:
PySide.QtCore.bool
Inserts the record after row . If row is negative, the record will be appended to the end. Calls
PySide.QtSql.QSqlTableModel.insertRows()
andPySide.QtSql.QSqlTableModel.setRecord()
internally.Returns true if the row could be inserted, otherwise false.
从上面我们可以得出结论,我们应该只使用 as row = -1。
self.dbModel.insertRecord(-1, record)
最好的办法是创建模型一次,而不是每次调用 doStore 函数时,我们还必须在 record() 函数的帮助下使用模型的 QSqlRecord,因为它加载了字段名称。
class MainWindow(QMainWindow):
def __init__(self, parent=None):
super(MainWindow, self).__init__(parent)
self.db = QSqlDatabase.addDatabase('QSQLITE')
self.db.setDatabaseName('example1.sqlite')
self.db.open()
self.db.transaction()
self.db.exec_(
'CREATE TABLE t1'
'(id INTEGER PRIMARY KEY, f1 INTEGER NOT NULL, f2 INTEGER NOT NULL, f3 TEXT NOT NULL)'
)
self.db.commit()
self.db.exec_("INSERT INTO t1 VALUES(1, 10, 20, 'db works fine')")
#Create User Interface
[...]
self.setCentralWidget(widget)
self.dbModel = QSqlTableModel(self)
self.dbModel.setTable('t1')
def doStore(self):
record = self.dbModel.record()
record.setValue(1, int(self.f1.text()))
record.setValue(2, int(self.f2.text()))
record.setValue(3, self.f3.text())
if not self.dbModel.insertRecord(-1, record):
print(self.db.lastError().text())
关于python - 在 PySide 中将单条记录写入 SQL 数据库,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46372957/