[PyQt] QSql lastError() must be called BEFORE exec_() ??

Mario Daniel Carugno carugnom at gmail.com
Fri Sep 4 21:23:32 BST 2009

Hi list, i'm coding my first PyQt4-Sql application, and i can't
understand a strange behavior.
I'll write a snippet of code with an erroneous Sql syntax, and i hope
then to catch the error
text and display it, in order to know what happened:

query = QSqlQuery()
query.prepare("insert inta table (name) values ('myname')")  # note
that 'inta' is an error
if query.isActive() == False:
   print "ERRSQL " + str(g_session.db.lastError().text())

And this do not work ! It is not displaying any text.
To get it work, i must get the error string BEFORE the execution of the query !!

query = QSqlQuery()
query.prepare("insert inta table (name) values ('myname')")  # i made
an erroneous sql
errorstr = str(query.lastError().text())
if query.isActive() == False:
   print "ERRSQL " + errorstr

Is that behavior normal ?? How can PyQt get the error's text BEFORE
executing the sql
statement ?

This is very strange for me, don't you think ?
It toke to me a long time to figure out how that works, because it
don't seems to be
logic. I mean, it's supposed that errors can be catched AFTER the
execution of a statement,

Well, i just would like to hear why this works that way, if someone
can explain it properly.


More information about the PyQt mailing list