[SQL] MySQL error codes

  • Hi!

    I'm writing a simple application that connects to database. Everything works fine but there's one thing that I can't solve myself.

    Let say that I have a query like that:
    QSqlQuery query("UPDATE table SET id=3 WHERE name='name'")

    This table already has record with id=3 (id is primary key) and mysql returns with code:
    #1062 - Duplicate entry '3' for key 'PRIMARY'

    Hovewer, when I execute this query by:
    if (!query.exec())
    QMessageBox::warning(this, "Database error", QString::number(db.lastError().number()));

    I get -1 instead of error code (db.lastError().text() is empty, too). Any ideas what's wrong? How can I get mysql error code after query failed? I need to know what was the reason of the error in order to (for example) inform user about it.

    Thanks in advance.

  • I think I can answer myself :)

    Instead of

    @db.lastError().number()@ should be @query.lastError().number()@

    db (QSqlDatabase) refers probably to whole database and that's why it doesn't return error codes of unsuccessful executed queries.

    Topic can be closed.

Log in to reply

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.