Python 在PySide中将单个记录写入SQL数据库
我想从多个QT小部件构建一个记录,并将其存储在数据库中。以下脚本运行时没有错误,但不存储记录。部分问题是我无法确定数据库中已经存在的最后一个ID,但即使我手动设置了ID,也不会写入任何内容。我在网上看到过使用insertRecord的示例,但QT文档建议使用insertRow。请随时纠正我的做法。我不熟悉Python和QtPython 在PySide中将单个记录写入SQL数据库,python,qt,pyside,Python,Qt,Pyside,我想从多个QT小部件构建一个记录,并将其存储在数据库中。以下脚本运行时没有错误,但不存储记录。部分问题是我无法确定数据库中已经存在的最后一个ID,但即使我手动设置了ID,也不会写入任何内容。我在网上看到过使用insertRecord的示例,但QT文档建议使用insertRow。请随时纠正我的做法。我不熟悉Python和Qt import sys from PySide.QtCore import * from PySide.QtGui import * from PySide.QtSql imp
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_())
如果要在表的末尾插入行,则无需指示索引,因为根据: PySide.QtSql.QSqlTableModel.insertRecord(行,记录) 参数:
row - PySide.QtCore.int
record – PySide.QtSql.QSqlRecord
返回类型:
PySide.QtCore.bool
在行后插入记录。如果行为负数,则记录将被删除
附加到末尾。调用PySide.QtSql.QSqlTableModel.insertRows()
和PySide.QtSql.QSqlTableModel.setRecord()
如果可以插入行,则返回true,否则返回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())
插入后是否必须commit()
?我也不知道Qt
,但您确实在这里提交:self.db.commit()
创建表后。是否要添加ID增加的元素?lastInsertId()
调用返回None
,因为模型本身没有进行任何插入。@roganjosh,感谢您的查看。我在创建数据库时使用原始SQL,而不是Qt模型。我认为commit()必须结束事务并写入数据。@ekhumaro,再次感谢您的帮助。我误解了lastInsertId()的功能。谢谢!我忽略了插入记录中的-1。这就解决了问题。