[PyQt] View made updatable by trigger - no update in QTableView

Sibylle Koczian Sibylle.Koczian at t-online.de
Mon Jan 28 21:27:53 GMT 2008


Hello,

the firebird example database contains a view which is read-only by nature. 
But by adding a trigger I made some of its fields editable. That works in the 
command line client, but I can't get it to work in a PyQt example program.

I took the "cachedtable.py" example, changed the "createConnection()" function 
and put the name of the view into the "setTable()" call of the 
QSqlTableModel. If I change a value in one of the columns that should now be 
editable, I get this error message: 
"The database reported an error: Unsuccessful execution caused by system error
that does not preclude successful execution of subsequent statements Unable to
execute query"

Here is the complete example (without the Trolltech Copyright notice):

#!/bin/env python

import sys
from PyQt4 import QtCore, QtGui, QtSql

def createConnection():
    db = QtSql.QSqlDatabase.addDatabase("QIBASE")
    db.setDatabaseName("localhost:employee")
    db.setUserName('sysdba')
    db.setPassword('masterkey')
    if not db.open():
        print "Datenbank lässt sich nicht öffnen."
        return False
    print "Datenbank offen."
    return True

class TableEditor(QtGui.QDialog):
    def __init__(self, tableName, parent = None):
        QtGui.QDialog.__init__(self, parent)
        
        self.model = QtSql.QSqlTableModel(self)
        self.model.setTable(tableName)
        self.model.setEditStrategy(QtSql.QSqlTableModel.OnManualSubmit)
        self.model.select()
        
        self.model.setHeaderData(0, QtCore.Qt.Horizontal,
                                 QtCore.QVariant(self.tr("ID")))
        self.model.setHeaderData(1, QtCore.Qt.Horizontal,
                                 QtCore.QVariant(self.tr("First name")))
        self.model.setHeaderData(2, QtCore.Qt.Horizontal,
                                 QtCore.QVariant(self.tr("Last name")))
        self.model.setHeaderData(3, QtCore.Qt.Horizontal,
                                 QtCore.QVariant(self.tr("Phone Ext")))
        self.model.setHeaderData(4, QtCore.Qt.Horizontal,
                                 QtCore.QVariant(self.tr("Dept. Location")))
        self.model.setHeaderData(5, QtCore.Qt.Horizontal,
                                 QtCore.QVariant(self.tr("Dept. Phone No")))
        
        view = QtGui.QTableView()
        view.setModel(self.model)
        
        self.submitButton = QtGui.QPushButton(self.tr("Submit"))
        self.submitButton.setDefault(True)
        self.revertButton = QtGui.QPushButton(self.tr("&Revert"))
        self.quitButton = QtGui.QPushButton(self.tr("Quit"))
        
        self.connect(self.submitButton, QtCore.SIGNAL("clicked()"), 
self.submit)
        self.connect(self.revertButton, QtCore.SIGNAL("clicked()"), 
                     self.model.revertAll)
        self.connect(self.quitButton, QtCore.SIGNAL("clicked()"), self.close)
        
        buttonLayout = QtGui.QVBoxLayout()
        buttonLayout.addWidget(self.submitButton)
        buttonLayout.addWidget(self.revertButton)
        buttonLayout.addWidget(self.quitButton)
        buttonLayout.addStretch(1)
        
        mainLayout = QtGui.QHBoxLayout()
        mainLayout.addWidget(view)
        mainLayout.addLayout(buttonLayout)
        self.setLayout(mainLayout)
        
        self.setWindowTitle(self.tr("Cached Table"))
        
    def closeEvent(self,  event):
        print "Jetzt ist Schluss."
        QtSql.QSqlDatabase.database().close()
        event.accept()
        
    def submit(self):
        self.model.database().transaction()
        if self.model.submitAll():
            self.model.database().commit()
        else:
            self.model.database().rollback()
            QtGui.QMessageBox.warning(self, self.tr("Cached Table"),
                        self.tr("The database reported an error: %1")
                                .arg(self.model.lastError().text()))

if __name__ == "__main__":
    app = QtGui.QApplication(sys.argv)
    if not connection_fb.createConnection():
        sys.exit(1)
    editor = TableEditor("phone_list")
    editor.show()
    sys.exit(app.exec_())


What is wrong? I will try to reproduce the problem on SQLITE, but that may 
take a little time.

Thank you,
Sibylle

-- 
Dr. Sibylle Koczian



More information about the PyQt mailing list