Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. [SQL] MySQL error codes
Forum Updated to NodeBB v4.3 + New Features

[SQL] MySQL error codes

Scheduled Pinned Locked Moved General and Desktop
2 Posts 1 Posters 3.0k Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • B Offline
    B Offline
    Baca48
    wrote on last edited by
    #1

    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.

    1 Reply Last reply
    0
    • B Offline
      B Offline
      Baca48
      wrote on last edited by
      #2

      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.

      1 Reply Last reply
      0

      • Login

      • Login or register to search.
      • First post
        Last post
      0
      • Categories
      • Recent
      • Tags
      • Popular
      • Users
      • Groups
      • Search
      • Get Qt Extensions
      • Unsolved