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

David Boddie david at boddie.org.uk
Sat Sep 5 00:04:11 BST 2009


On Fri Sep 4 21:23:32 BST 2009, Mario Daniel Carugno wrote:

> 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
> query.exec_()
> if query.isActive() == False:
>    print "ERRSQL " + str(g_session.db.lastError().text())
>
> And this do not work ! It is not displaying any text.

Aren't you calling the QSqlDatabase's query method here?

http://www.riverbankcomputing.co.uk/static/Docs/PyQt4/html/qsqldatabase.html#lastError

> 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
> query.exec_()
> errorstr = str(query.lastError().text())
> if query.isActive() == False:
>    print "ERRSQL " + errorstr

Here you get the error string after execution!

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

I don't think you can. The prepare() documentation says that different
databases behave in different ways:

http://www.riverbankcomputing.co.uk/static/Docs/PyQt4/html/qsqlquery.html#prepare

Hopefully, someone with SQL experience can give you some hints.

David


More information about the PyQt mailing list