[PyQt] Insertrow with QSqlTableModel doesn't work

Volker Helm Helm.volker at gmx.de
Fri May 22 20:47:06 BST 2009


Hi there,

i've got a problem. In my class enderNewIdOrder(QtGui.QDialog) the insertRow  doesn't work. I did as in "Rapid GUI Programming with Python and Qt" discribed. But the insert into the Database PostgreSQL over ODBC doesn't work. Regular changes are applied.

The application based on QDialog. It has two View (customerView representing customerModel=QSqlQuery() and projectView representing projectModel = QSqlTableModel())

Here comes my function

    def addProject(self):
        customerindex = self.customerView.currentIndex()
        if not customerindex.isValid():
            return
        QtSql.QSqlDatabase.database().transaction()
        record = self.customerModel.record(customerindex.row())
        # getting foreign key
        idpartner = record.value(0).toInt()[0]
        
        # getting new primary key
        query = QtSql.QSqlQuery()
        query.exec_("SELECT MAX(idproject) FROM project")
        if query.next():
            idproject = query.value(0).toInt()[0]+1
        
        row = self.projectModel.rowCount()
        self.projectModel.insertRow(row)
                      QtCore.QVariant(QtCore.QDate.currentDate()))
        self.projectModel.setData(self.projectModel.index(row, 0), QtCore.QVariant(idproject))
        self.projectModel.setData(self.projectModel.index(row, 1), QtCore.QVariant(QtCore.QString(u"New Project")))
        self.projectModel.setData(self.projectModel.index(row, 2), QtCore.QVariant(1))
        self.projectModel.setData(self.projectModel.index(row, 3), QtCore.QVariant(idpartner))
        self.projectModel.setData(self.projectModel.index(row, 4), QtCore.QVariant(0))
        self.projectModel.setData(self.projectModel.index(row, 5), QtCore.QVariant(0))
        self.projectModel.setData(self.projectModel.index(row, 6), QtCore.QVariant(QtCore.QDateTime.currentDateTime()))

        QtSql.QSqlDatabase.database().commit()
        index = self.projectModel.index(row, 1)
        self.projectView.setCurrentIndex(index)
        self.projectView.edit(index)


I hope anybody ould give me a hint, what I'm doing wrong. It cost me the last 10 hours.
If I insert the row manualy by inserting folling code between the transaction. I get the new Line, but I can edit the content only after I left the current customer and reread the actual customer.

Here the manual insert

query.prepare("INSERT INTO project (idproject,description ,idunit, idpartner,idaddress, statusid,created ) "
                      "VALUES (:idproject, :description,:idunit,:idpartner,null,0, now())")
        query.bindValue(":idproject", QtCore.QVariant(idproject))
        query.bindValue(":description", QtCore.QVariant(QtCore.QVariant(QtCore.QString("Neues Projekt"))))
        query.bindValue(":idunit", QtCore.QVariant(1))
        query.bindValue(":idpartner", QtCore.QVariant(idpartner))
        query.exec_()

Thanks in advance,

Volker
-- 
Neu: GMX FreeDSL Komplettanschluss mit DSL 6.000 Flatrate + Telefonanschluss für nur 17,95 Euro/mtl.!* http://portal.gmx.net/de/go/dsl02


More information about the PyQt mailing list