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. QSqlQuery lastInsertId()
Forum Updated to NodeBB v4.3 + New Features

QSqlQuery lastInsertId()

Scheduled Pinned Locked Moved Solved General and Desktop
7 Posts 3 Posters 3.4k Views
  • 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.
  • CodeFreaksC Offline
    CodeFreaksC Offline
    CodeFreaks
    wrote on last edited by
    #1

    Hi everyone,
    I tried to get last id through QSqlQuery::lastInsertId().

        QSqlDatabase myDb = QSqlDatabase::addDatabase("QMYSQL");
        myDb.setHostName    ("localhost");
        myDb.setUserName    ("root");
        myDb.setPassword    ("");
        myDb.setDatabaseName("testdatabase");
    
        qDebug()<<"Started!";
    
        if(myDb.open()){
            qDebug()<<"Database Connected!";
        }else{
            qDebug()<<myDb.lastError();
        }
    
        bool avFeature = myDb.driver()->hasFeature(QSqlDriver::LastInsertId);
        qDebug()<<"LastInsertedId is availabe: " << avFeature;
    
        QSqlQuery myQuery(myDb);
        if (myQuery.exec("SELECT id FROM users"))
        qDebug()<<"Last ID: "<<myQuery.lastInsertId();
    

    Output:

    Started!
    Database Connected!
    LastInsertedId is availabe:  true
    Last ID:  QVariant(Invalid)
    

    Database:
    0_1535713009389_Capture.PNG

    JonBJ 1 Reply Last reply
    0
    • CodeFreaksC CodeFreaks

      Hi everyone,
      I tried to get last id through QSqlQuery::lastInsertId().

          QSqlDatabase myDb = QSqlDatabase::addDatabase("QMYSQL");
          myDb.setHostName    ("localhost");
          myDb.setUserName    ("root");
          myDb.setPassword    ("");
          myDb.setDatabaseName("testdatabase");
      
          qDebug()<<"Started!";
      
          if(myDb.open()){
              qDebug()<<"Database Connected!";
          }else{
              qDebug()<<myDb.lastError();
          }
      
          bool avFeature = myDb.driver()->hasFeature(QSqlDriver::LastInsertId);
          qDebug()<<"LastInsertedId is availabe: " << avFeature;
      
          QSqlQuery myQuery(myDb);
          if (myQuery.exec("SELECT id FROM users"))
          qDebug()<<"Last ID: "<<myQuery.lastInsertId();
      

      Output:

      Started!
      Database Connected!
      LastInsertedId is availabe:  true
      Last ID:  QVariant(Invalid)
      

      Database:
      0_1535713009389_Capture.PNG

      JonBJ Offline
      JonBJ Offline
      JonB
      wrote on last edited by JonB
      #2

      @CodeFreaks said in QSqlQuery lastInsertId():

      QSqlQuery::lastInsertId()

      gives you the ID from the last row inserted via an INSERT statement immediately preceding on that QSqlQuery (so that you can tell which row it just inserted). I don't know what you think you're trying to do here, but since you do no INSERT you'll not get any valid value back.

      P.S. Do you want to know the highest id column value currently in that table? Then just myQuery.exec("SELECT MAX(id) FROM users") returns it.

      CodeFreaksC 1 Reply Last reply
      3
      • JonBJ JonB

        @CodeFreaks said in QSqlQuery lastInsertId():

        QSqlQuery::lastInsertId()

        gives you the ID from the last row inserted via an INSERT statement immediately preceding on that QSqlQuery (so that you can tell which row it just inserted). I don't know what you think you're trying to do here, but since you do no INSERT you'll not get any valid value back.

        P.S. Do you want to know the highest id column value currently in that table? Then just myQuery.exec("SELECT MAX(id) FROM users") returns it.

        CodeFreaksC Offline
        CodeFreaksC Offline
        CodeFreaks
        wrote on last edited by CodeFreaks
        #3

        @JonB
        Thanks for reply.
        I want to get the maximum (last) allocated id within a specific table in the database, as a variable.

        JonBJ 1 Reply Last reply
        0
        • CodeFreaksC CodeFreaks

          @JonB
          Thanks for reply.
          I want to get the maximum (last) allocated id within a specific table in the database, as a variable.

          JonBJ Offline
          JonBJ Offline
          JonB
          wrote on last edited by
          #4

          @CodeFreaks
          So that's what my "P.S." guess above gave you....

          CodeFreaksC 1 Reply Last reply
          0
          • JonBJ JonB

            @CodeFreaks
            So that's what my "P.S." guess above gave you....

            CodeFreaksC Offline
            CodeFreaksC Offline
            CodeFreaks
            wrote on last edited by CodeFreaks
            #5

            @JonB
            I don't know that what's wrong.

                if (myQuery.exec("SELECT MAX(id) FROM users"))
                qDebug()<<"Last ID: "<<myQuery.value(0);
            

            Output:

            Database Connected!
            LastInsertedId is availabe:  true
            Last ID:  QVariant(Invalid)
            
            1 Reply Last reply
            0
            • Christian EhrlicherC Offline
              Christian EhrlicherC Offline
              Christian Ehrlicher
              Lifetime Qt Champion
              wrote on last edited by
              #6

              @CodeFreaks said in QSqlQuery lastInsertId():

              I don't know that what's wrong.

              You forgot to retrieve the data with .next() - see http://doc.qt.io/qt-5/qsqlquery.html#details

              Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
              Visit the Qt Academy at https://academy.qt.io/catalog

              CodeFreaksC 1 Reply Last reply
              3
              • Christian EhrlicherC Christian Ehrlicher

                @CodeFreaks said in QSqlQuery lastInsertId():

                I don't know that what's wrong.

                You forgot to retrieve the data with .next() - see http://doc.qt.io/qt-5/qsqlquery.html#details

                CodeFreaksC Offline
                CodeFreaksC Offline
                CodeFreaks
                wrote on last edited by
                #7
                This post is deleted!
                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