[PyQt] SQLite commit success, but data isn't modified

Alessandro Re akirosspower at gmail.com
Thu Nov 6 10:48:55 GMT 2008


Hello,
I'm trying to using QSqlTableModel on a SQLite database.
Following the Qt Cached Table Example, I did a simple program (see below):
pressing a button should commit table's changes to the database.
But pressing it, the table return to original data, even if the
submitAll() method return true.

I'd like to check at low level how Qt talk to SQLite (e.g. the query
sent), is there a method?
Thanks

#!/usr/bin/python
import sys
from PyQt4.QtGui import *
from PyQt4.QtSql import *
from PyQt4.QtCore import *

app = QApplication(sys.argv)
main = QMainWindow()

dbName = "testing"
database = QSqlDatabase.addDatabase("QSQLITE")
database.setDatabaseName(dbName)
if (not database.open()):
	print("Can't open database '" + dbName + "'")
	sys.exit(1)

# Model for inventory table
inventoryModel = QSqlTableModel(main, database)
inventoryModel.setEditStrategy(QSqlTableModel.OnManualSubmit)
inventoryModel.setTable("inventory")
inventoryModel.select()
tableInventory = QTableView()
tableInventory.setModel(inventoryModel)

submit = QPushButton("Submit")

def saveInventory():
	if (not database.transaction()):
		print("Can't begin transaction")
		return
	if (inventoryModel.submitAll()):
		database.commit()
		print("Commit ok")
	else:
		database.rollback()
		print("Error occurred: " + self.inventoryModel.lastError().text())
	
QObject.connect(submit, SIGNAL("clicked()"), saveInventory)

# Central widget
cw = QWidget()
cl = QVBoxLayout()
cl.addWidget(submit)
cl.addWidget(tableInventory)

cw.setLayout(cl)

main.setCentralWidget(cw)

main.show()
sys.exit(app.exec_())


More information about the PyQt mailing list