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. QSqlError.number() always returns -1
Qt 6.11 is out! See what's new in the release blog

QSqlError.number() always returns -1

Scheduled Pinned Locked Moved Solved General and Desktop
12 Posts 4 Posters 2.1k Views 2 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.
  • C Offline
    C Offline
    chraz
    wrote on last edited by
    #3

    Hello @SGaist
    Here is the code :

    int Project::insert()
    {
        QSqlQuery query = DbConnector::getQuery();
        query.prepare("INSERT INTO project(name, description, color) VALUES(:n, :d, :c)");
        query.bindValue(":n", name);
        query.bindValue(":d", description);
        query.bindValue(":c", color);
        if(!query.exec())
        {
            qDebug() << query.lastError().number();
        }
        return query.lastInsertId().toInt();
    }
    

    I intentionnally droped the table, so I expect to see a kind of "No such table" or something like that.

    jsulmJ 1 Reply Last reply
    0
    • C chraz

      Hello @SGaist
      Here is the code :

      int Project::insert()
      {
          QSqlQuery query = DbConnector::getQuery();
          query.prepare("INSERT INTO project(name, description, color) VALUES(:n, :d, :c)");
          query.bindValue(":n", name);
          query.bindValue(":d", description);
          query.bindValue(":c", color);
          if(!query.exec())
          {
              qDebug() << query.lastError().number();
          }
          return query.lastInsertId().toInt();
      }
      

      I intentionnally droped the table, so I expect to see a kind of "No such table" or something like that.

      jsulmJ Offline
      jsulmJ Offline
      jsulm
      Lifetime Qt Champion
      wrote on last edited by
      #4

      @chraz What happens if you try to do the same on SQL console?

      https://forum.qt.io/topic/113070/qt-code-of-conduct

      1 Reply Last reply
      1
      • C Offline
        C Offline
        chraz
        wrote on last edited by
        #5
        INSERT INTO project(name, description, color) VALUES("P1", "D1", "#000000");
        

        Got error :

        Error while executing SQL query on database 'todos': no such table: project
        
        1 Reply Last reply
        0
        • SGaistS Offline
          SGaistS Offline
          SGaist
          Lifetime Qt Champion
          wrote on last edited by
          #6

          Which version of Qt are you using ?

          Interested in AI ? www.idiap.ch
          Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

          1 Reply Last reply
          0
          • C Offline
            C Offline
            chraz
            wrote on last edited by
            #7

            Desktop Qt 5.2.0 MinGW 32bit

            1 Reply Last reply
            0
            • mranger90M Offline
              mranger90M Offline
              mranger90
              wrote on last edited by
              #8

              Did you check the return of the query.prepare call ?
              Perhaps you are not connected to the database.

              1 Reply Last reply
              1
              • C Offline
                C Offline
                chraz
                wrote on last edited by
                #9
                int Project::insert()
                {
                    QSqlQuery query = DbConnector::getQuery();
                    if(!query.prepare("INSERT INTO project(name, description, color) VALUES(:n, :d, :c)"))
                    {
                        qDebug() << "PREPARE : " << query.lastError().text();
                        return 0;
                    }
                    query.bindValue(":n", name);
                    query.bindValue(":d", description);
                    query.bindValue(":c", color);
                    if(!query.exec())
                    {
                        qDebug() << query.lastError().number();
                    }
                    return query.lastInsertId().toInt();
                }
                

                So it gaves me :

                PREPARE :  "no such table: project Unable to execute statement" 
                

                @mranger90 , thanks for your help.
                I'll mark it as resolved.

                1 Reply Last reply
                0
                • SGaistS Offline
                  SGaistS Offline
                  SGaist
                  Lifetime Qt Champion
                  wrote on last edited by
                  #10

                  On a side note, support for named placeholder with SQLite requires a more recent version of Qt.

                  Interested in AI ? www.idiap.ch
                  Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

                  1 Reply Last reply
                  0
                  • C Offline
                    C Offline
                    chraz
                    wrote on last edited by
                    #11

                    Can you explain a little more ?

                    1 Reply Last reply
                    0
                    • SGaistS Offline
                      SGaistS Offline
                      SGaist
                      Lifetime Qt Champion
                      wrote on last edited by
                      #12

                      Proper handling of named placeholder (e.g. the :n you are using) has been implemented recently and you are using an old 5.2.0 release current is 5.10.1 with 5.11 around the corner.

                      Interested in AI ? www.idiap.ch
                      Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

                      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