qsqltablemodel.insertrecord(row,record)未插入指定行
使用提供的答案在这里,我能够插入记录进入我的QSQLTableModel。虽然此记录将记录附加到末端(行= -1),但更改行不会在插入记录的位置更改。如何使记录在指定的行中插入?
import sys
from PySide6.QtWidgets import (QApplication, QMainWindow, QTableView)
from PySide6.QtSql import QSqlDatabase, QSqlTableModel, QSqlQuery
class SQLTableModel(QSqlTableModel):
def __init__(self, parent=None):
super().__init__(parent)
query = QSqlQuery()
query.exec(
"CREATE TABLE table1"
"(id INTEGER PRIMARY KEY AUTOINCREMENT UNIQUE NOT NULL, "
"type TEXT)"
)
class MainWindow(QMainWindow):
def __init__(self):
super().__init__()
self.db = QSqlDatabase.addDatabase("QSQLITE")
self.db.setDatabaseName("SQLTableDatabase.sqlite")
self.db.open()
self.table_view = QTableView()
self.model = SQLTableModel()
self.table_view.setModel(self.model)
self.model.setTable("table1")
for i in range(4):
record = self.model.record()
record.setValue("type", str(i))
self.model.insertRecord(-1, record)
record = self.model.record()
record.setValue("type", "TEST")
self.model.insertRecord(2, record) # Why is this record not inserted at row 2?
self.model.select()
self.setCentralWidget(self.table_view)
app = QApplication(sys.argv)
mainwindow = MainWindow()
mainwindow.show()
sys.exit(app.exec())
Using the answer provided here, I was able to insert records into my QSqlTableModel. While this appends records to the end (row=-1) just fine, changing the row does not change where the record is inserted. How do I cause a record to be inserted at the specified row?
import sys
from PySide6.QtWidgets import (QApplication, QMainWindow, QTableView)
from PySide6.QtSql import QSqlDatabase, QSqlTableModel, QSqlQuery
class SQLTableModel(QSqlTableModel):
def __init__(self, parent=None):
super().__init__(parent)
query = QSqlQuery()
query.exec(
"CREATE TABLE table1"
"(id INTEGER PRIMARY KEY AUTOINCREMENT UNIQUE NOT NULL, "
"type TEXT)"
)
class MainWindow(QMainWindow):
def __init__(self):
super().__init__()
self.db = QSqlDatabase.addDatabase("QSQLITE")
self.db.setDatabaseName("SQLTableDatabase.sqlite")
self.db.open()
self.table_view = QTableView()
self.model = SQLTableModel()
self.table_view.setModel(self.model)
self.model.setTable("table1")
for i in range(4):
record = self.model.record()
record.setValue("type", str(i))
self.model.insertRecord(-1, record)
record = self.model.record()
record.setValue("type", "TEST")
self.model.insertRecord(2, record) # Why is this record not inserted at row 2?
self.model.select()
self.setCentralWidget(self.table_view)
app = QApplication(sys.argv)
mainwindow = MainWindow()
mainwindow.show()
sys.exit(app.exec())
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
由于其性质,数据库没有在给定索引上插入的概念:记录始终附加。
默认
editstrategy该模型实际上将更改提交到数据库中)为
onRowchange
,每当插入新记录时,这也会发生。结果是,当您到达for循环的末尾,该模型已经提交了更改,并且已插入了新记录,因此,无论您使用什么
row
,数据库都只能插入新记录最后。在给定的行中插入记录的唯一(简单)方法是在插入以前的情况下做到这一点,但 not art art 都使用
on ManualSubmit
策略。在您的情况下:
这将正确插入第2行的最后一个记录。
但是,如果以前的记录已经存在于数据库中(或以前提交),则无能为表或手动
Update
通过“将”每个记录的现有数据转移到以下行,所有记录都从通缉行开始。Due to their nature, databases don't have the concept of insertion at a given index: records are always appended.
The default
editStrategy
(which dictates when the model actually submits changes to the database) isOnRowChange
, and that also happens whenever a new record is inserted.The result is that when you reach the end of the for loop, the model has already submitted the changes and the new records have been inserted, so, no matter what
row
you use, the database can only insert new records at the end.The only (simple) way to insert records at a given row is to do that while previous have been inserted but not submitted yet, and using the
OnManualSubmit
strategy.In your case:
This will properly insert the last record at row 2.
But, if the previous records already existed in the database (or were previously submitted), there's nothing you can do, except from regenerate the table, or manually
UPDATE
all records starting from the wanted row, by "shifting" the existing data of each record to the following row.