On Sep 4, 2009, at 1:23 PM, 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.
To get it work, i must get the error string BEFORE the execution of the query !!

I believe you might be getting the correct lastError() string, but from the previous time you ran your test.


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

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

I only use query.prepare() on rare occasions when I need to use addBindValue() to achieve automagic character escaping. eg:

    query.prepare("INSERT INTO foo ... ")
    query.addBindValue(QtCore.QVariant(QtCore.QString("%s" % bar)))
    err = query.exec_()
    print "err: ", err

My typical handling of SQL calls is quite simple. I create a new QSqlQuery object, pass it an SQL query string, then execute it. eg:

    query    = QtSql.QSqlQuery()
    queryStr = "INSERT INTO foo ..."
    query.exec_(queryStr)
    print "lastError: ",  query.lastError().text()

Hope this helps,
Scott






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,
not BEFORE.

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

Thanks
_______________________________________________
PyQt mailing list    PyQt@riverbankcomputing.com
http://www.riverbankcomputing.com/mailman/listinfo/pyqt









_______________________________________________
PyQt mailing list    PyQt@riverbankcomputing.com
http://www.riverbankcomputing.com/mailman/listinfo/pyqt

Reply via email to