PDA

View Full Version : QSqlQuery error



skuda
1st November 2007, 15:12
Hello,
i have a problem with QSqlQuery, i have a column "nombre" with an unique index (constraint), i am doing many inserts from PyQT (ODBC driver), i have the same problem from windows client and linux client, when any of the inserts of the prepared query have a duplicated unique column all the executed later gives me the same error but the insert works, i will explain here:

i prepare the query:

queryinsertmaxdb = QSqlQuery(maxdb)
queryinsertmaxdb.prepare("INSERT INTO NAMESPACE.MODELO_FAMILIA (FAMILIA_ID, NOMBRE, SECCION) VALUES (?, ?, ?)")

later inside a loop for every value in a list i do this:

queryinsertmaxdb.addBindValue(QVariant(codigo_fami lia))
queryinsertmaxdb.addBindValue(QVariant(nombre_fami lia))
queryinsertmaxdb.addBindValue(QVariant(seccion_fam ilia))
queryinsertmaxdb.exec_()

all works fine but when it finds an integrity constraint violation:
INSERT INTO NAMESPACE.MODELO_FAMILIA (FAMILIA_ID, NOMBRE, SECCION) VALUES (8325, TENIS MUJER, 2)
Unable to execute statement: [SAP AG][LIBSQLOD SO][MaxDB] Integrity constraint violation;250 POS(1) Duplicate secondary key:UQ_NOMBRE [SAP AG][LIBSQLOD SO][MaxDB] Integrity constraint violation;250 POS(1) Duplicate secondary key:UQ_NOMBRE

this insert does not works and it is correct (because it has the column name duplicated) but all the inserts after this gives me the same errors and all of them do correctly the insert because dont have a duplicate
secondary key really, what can be the problem? I suppose with qsqlquery exec method reset the errors of last query but i thinks it is not working. Thanks in advance.

jacek
1st November 2007, 23:01
I suppose with qsqlquery exec method reset the errors of last query but i thinks it is not working.
QSqlQuery::lastError() returns the last error that has occured, even if there were some successful queries afterwards. You have to check what QSqlQuery::exec() returns to see whether the error has occurred or not.

skuda
2nd November 2007, 08:43
But in qt documentation says it is reseted in every exec method, here i paste the doc about ::exec.

bool QSqlQuery::exec ( const QString & query )
Executes the SQL in query. Returns true and sets the query state to active if the query was successful; otherwise returns false. The query string must use syntax appropriate for the SQL database being queried (for example, standard SQL).
After the query is executed, the query is positioned on an invalid record and must be navigated to a valid record before data values can be retrieved (for example, using next()).
Note that the last error for this query is reset when exec() is called.

When i do the prepare inside the loop it works ok, i think it is a qt bug, i have posted to the tasktracker and i am awaiting reply.