[PyQt] Problem when inserted rows into QSqlRelationalTableModel
with over 256 rows
Catriona Johnson
catriona.johnson at gmail.com
Mon Jul 27 05:10:34 BST 2009
Hi
I have an application with over a dozen tables and models on each.
Inserts, updates, selects and deletes work fine until one of my models
has over 256 rows. If I use query().finish() on the model things work,
except if it is a relation to another model, when I have to call
query().finish() on the relation as well.
This doesn't seem like a good way to go about things, so any
suggestions on what I am doing wrong would be appreciated. Should I
look at multiple threads?
Below is a small application that shows the problem.
Thanks in advance
Catriona
## Seems that problem occurs when there is a table (and optionally a
relation to that table)
## with more than 256 rows when the application is opened
import sys
from PyQt4.QtCore import *
from PyQt4.QtGui import *
from PyQt4.QtSql import *
def main():
def clearQuery(model):
if model.query().isActive():
model.query().finish()
app = QApplication(sys.argv)
# Create database
db = QSqlDatabase.addDatabase("QSQLITE")
db.setDatabaseName("test.db")
db.open()
query = QSqlQuery()
query.exec_("CREATE TABLE Relation("
"RelationId INTEGER PRIMARY KEY AUTOINCREMENT UNIQUE NOT NULL, "
"Relation VARCHAR NULL"
")")
query.prepare("INSERT INTO Relation(Relation) VALUES (?)")
for i in range (0, 260): # If this is less than 256, insert will work
query.addBindValue(QVariant(i))
query.exec_()
query.exec_("CREATE TABLE InsertTable("
"InsertTableId INTEGER PRIMARY KEY AUTOINCREMENT UNIQUE NOT NULL, "
"RelationId INTEGER NULL CONSTRAINT fk_relation REFERENCES
Relation(RelationId)"
")")
query.prepare("INSERT INTO InsertTable(RelationId) VALUES (?)")
for i in range (0, 6):
query.addBindValue(QVariant(1))
query.exec_()
# Now create model
insertModel = QSqlRelationalTableModel()
insertModel.setTable("InsertTable")
insertModel.setRelation(insertModel.fieldIndex("RelationId"),
QSqlRelation("Relation", "RelationId", "Relation"))
insertModel.select()
while insertModel.canFetchMore():
insertModel.fetchMore()
insertModelRelationModel = insertModel.relationModel(1)
# And do insert
row = insertModel.rowCount()
insertModel.insertRow(row)
insertModel.setData(insertModel.index(row, 1), QVariant(str(2)))
insertModel.database().transaction()
insertModel.submitAll()
clearQuery(insertModel) # If this is commented out insert will fail
#clearQuery(insertModelRelationModel) # If this is commented out
insert will fail
insertModel.database().commit()
main()
More information about the PyQt
mailing list