Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. Mobile and Embedded
  4. What is problem with qQuery.lastInsertId().toInt() ? why below function return -1 for u32FileId variable ?

What is problem with qQuery.lastInsertId().toInt() ? why below function return -1 for u32FileId variable ?

Scheduled Pinned Locked Moved Solved Mobile and Embedded
13 Posts 2 Posters 1.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.
  • Q Offline
    Q Offline
    Qt embedded developer
    wrote on last edited by
    #1

    QSqlError FileLog::InsertData(UINT32 &u32FileId)
    {
    FUNCTION_IN;
    QSqlQuery qQuery;

    qQuery.prepare("INSERT INTO "+sDBTableName[DBTABLE_FILELOG]+"\
                     (FilePath,\
                      FileType,\
                      FileTagName,\
                      Comment,\
                      InsertedDate,\
                      IsDirty)"
                            "VALUES (?, ?, ?, ?, ?, ?)");
    qQuery.addBindValue(sFilePath);
    qQuery.addBindValue(u8FileType);
    qQuery.addBindValue(sFileTagName);
    qQuery.addBindValue(sComment);
    qQuery.addBindValue(dCurrentDate);
    qQuery.addBindValue(u8Dirty);
    
    if(!pMainApp.ObjDbOperations.ExecuteQuery(qQuery))
        return qQuery.lastError();
    
    u32FileId = qQuery.lastInsertId().toInt();  //Get Last Auto Increment ID
    FUNCTION_OUT;
    return QSqlError();
    

    }

    jsulmJ Q 2 Replies Last reply
    0
    • Q Qt embedded developer

      QSqlError FileLog::InsertData(UINT32 &u32FileId)
      {
      FUNCTION_IN;
      QSqlQuery qQuery;

      qQuery.prepare("INSERT INTO "+sDBTableName[DBTABLE_FILELOG]+"\
                       (FilePath,\
                        FileType,\
                        FileTagName,\
                        Comment,\
                        InsertedDate,\
                        IsDirty)"
                              "VALUES (?, ?, ?, ?, ?, ?)");
      qQuery.addBindValue(sFilePath);
      qQuery.addBindValue(u8FileType);
      qQuery.addBindValue(sFileTagName);
      qQuery.addBindValue(sComment);
      qQuery.addBindValue(dCurrentDate);
      qQuery.addBindValue(u8Dirty);
      
      if(!pMainApp.ObjDbOperations.ExecuteQuery(qQuery))
          return qQuery.lastError();
      
      u32FileId = qQuery.lastInsertId().toInt();  //Get Last Auto Increment ID
      FUNCTION_OUT;
      return QSqlError();
      

      }

      jsulmJ Online
      jsulmJ Online
      jsulm
      Lifetime Qt Champion
      wrote on last edited by
      #2

      @Qt-embedded-developer According to https://doc.qt.io/qt-5/qsqlquery.html#lastInsertId not every database supports returning last insert id. You should check whether yours does using https://doc.qt.io/qt-5/qsqldriver.html#hasFeature

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

      1 Reply Last reply
      2
      • Q Qt embedded developer

        QSqlError FileLog::InsertData(UINT32 &u32FileId)
        {
        FUNCTION_IN;
        QSqlQuery qQuery;

        qQuery.prepare("INSERT INTO "+sDBTableName[DBTABLE_FILELOG]+"\
                         (FilePath,\
                          FileType,\
                          FileTagName,\
                          Comment,\
                          InsertedDate,\
                          IsDirty)"
                                "VALUES (?, ?, ?, ?, ?, ?)");
        qQuery.addBindValue(sFilePath);
        qQuery.addBindValue(u8FileType);
        qQuery.addBindValue(sFileTagName);
        qQuery.addBindValue(sComment);
        qQuery.addBindValue(dCurrentDate);
        qQuery.addBindValue(u8Dirty);
        
        if(!pMainApp.ObjDbOperations.ExecuteQuery(qQuery))
            return qQuery.lastError();
        
        u32FileId = qQuery.lastInsertId().toInt();  //Get Last Auto Increment ID
        FUNCTION_OUT;
        return QSqlError();
        

        }

        Q Offline
        Q Offline
        Qt embedded developer
        wrote on last edited by
        #3

        @Qt-embedded-developer

        so what i need to do to get the last auto increment id of data base in my case

        jsulmJ 1 Reply Last reply
        0
        • Q Qt embedded developer

          @Qt-embedded-developer

          so what i need to do to get the last auto increment id of data base in my case

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

          @Qt-embedded-developer Did you check whether your database supports this using https://doc.qt.io/qt-5/qsqldriver.html#hasFeature as I suggested?

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

          Q 1 Reply Last reply
          1
          • jsulmJ jsulm

            @Qt-embedded-developer Did you check whether your database supports this using https://doc.qt.io/qt-5/qsqldriver.html#hasFeature as I suggested?

            Q Offline
            Q Offline
            Qt embedded developer
            wrote on last edited by
            #5

            @jsulm yes it gives positive values but some time why -1 it return. actually it need to return 0 or positive last id

            jsulmJ 1 Reply Last reply
            0
            • Q Qt embedded developer

              @jsulm yes it gives positive values but some time why -1 it return. actually it need to return 0 or positive last id

              jsulmJ Online
              jsulmJ Online
              jsulm
              Lifetime Qt Champion
              wrote on last edited by
              #6

              @Qt-embedded-developer If it returns -1 is the new row actually inserted?

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

              Q 1 Reply Last reply
              1
              • jsulmJ jsulm

                @Qt-embedded-developer If it returns -1 is the new row actually inserted?

                Q Offline
                Q Offline
                Qt embedded developer
                wrote on last edited by
                #7

                @jsulm no new row not inserted but realtional data base table contains new row which has -1 value due to u32FileId

                jsulmJ 1 Reply Last reply
                0
                • Q Qt embedded developer

                  @jsulm no new row not inserted but realtional data base table contains new row which has -1 value due to u32FileId

                  jsulmJ Online
                  jsulmJ Online
                  jsulm
                  Lifetime Qt Champion
                  wrote on last edited by
                  #8

                  @Qt-embedded-developer said in What is problem with qQuery.lastInsertId().toInt() ? why below function return -1 for u32FileId variable ?:

                  no new row not inserted but realtional data base table contains new row which has -1

                  Sorry, I don't understand this.
                  If you execute the INSERT query - is there new row in your table afterwards or not?

                  "//Get Last Auto Increment ID" - do you have an autoincrement id column in that table?

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

                  Q 1 Reply Last reply
                  0
                  • jsulmJ jsulm

                    @Qt-embedded-developer said in What is problem with qQuery.lastInsertId().toInt() ? why below function return -1 for u32FileId variable ?:

                    no new row not inserted but realtional data base table contains new row which has -1

                    Sorry, I don't understand this.
                    If you execute the INSERT query - is there new row in your table afterwards or not?

                    "//Get Last Auto Increment ID" - do you have an autoincrement id column in that table?

                    Q Offline
                    Q Offline
                    Qt embedded developer
                    wrote on last edited by Qt embedded developer
                    #9

                    @jsulm

                    Qt embedded developer about 3 hours ago

                    QSqlError FileLog::InsertData(UINT32 &u32FileId)
                    {
                    FUNCTION_IN;
                    QSqlQuery qQuery;

                    qQuery.prepare("INSERT INTO "+sDBTableName[DBTABLE_FILELOG]+"
                    (FilePath,
                    FileType,
                    FileTagName,
                    Comment,
                    InsertedDate,
                    IsDirty)"
                    "VALUES (?, ?, ?, ?, ?, ?)");
                    qQuery.addBindValue(sFilePath);
                    qQuery.addBindValue(u8FileType);
                    qQuery.addBindValue(sFileTagName);
                    qQuery.addBindValue(sComment);
                    qQuery.addBindValue(dCurrentDate);
                    qQuery.addBindValue(u8Dirty);

                    if(!pMainApp.ObjDbOperations.ExecuteQuery(qQuery))
                    return qQuery.lastError();

                    u32FileId = qQuery.lastInsertId().toInt(); //Get Last Auto Increment ID
                    FUNCTION_OUT;
                    return QSqlError();

                    }


                    above function is called by below function
                    void Camera::MakeFileEntry(QString sFilePath, CAPTURED_FILETYPE eFileType)
                    {
                    FileLog *ObjFileLog = NULL;
                    ObjFileLog = new FileLog();
                    if(IS_VALID_OBJ(ObjFileLog))
                    {
                    ObjFileLog->sFilePath = sFilePath;
                    ObjFileLog->u8FileType = eFileType;
                    ObjFileLog->dCurrentDate = QDate::currentDate();
                    ObjFileLog->u8Dirty = DIRTYBIT_SET;

                        ObjFileLog->InsertData(u32CamFileId);
                        DELETE_OBJ(ObjFileLog);
                    }
                    

                    }


                    now we have used this u32CamFileId to insert in readinglog table

                    ObjReadingLog->u32FileId = u32CamFileId;


                    why above code return -1 for u32CamFileId ?

                    jsulmJ 1 Reply Last reply
                    0
                    • Q Qt embedded developer

                      @jsulm

                      Qt embedded developer about 3 hours ago

                      QSqlError FileLog::InsertData(UINT32 &u32FileId)
                      {
                      FUNCTION_IN;
                      QSqlQuery qQuery;

                      qQuery.prepare("INSERT INTO "+sDBTableName[DBTABLE_FILELOG]+"
                      (FilePath,
                      FileType,
                      FileTagName,
                      Comment,
                      InsertedDate,
                      IsDirty)"
                      "VALUES (?, ?, ?, ?, ?, ?)");
                      qQuery.addBindValue(sFilePath);
                      qQuery.addBindValue(u8FileType);
                      qQuery.addBindValue(sFileTagName);
                      qQuery.addBindValue(sComment);
                      qQuery.addBindValue(dCurrentDate);
                      qQuery.addBindValue(u8Dirty);

                      if(!pMainApp.ObjDbOperations.ExecuteQuery(qQuery))
                      return qQuery.lastError();

                      u32FileId = qQuery.lastInsertId().toInt(); //Get Last Auto Increment ID
                      FUNCTION_OUT;
                      return QSqlError();

                      }


                      above function is called by below function
                      void Camera::MakeFileEntry(QString sFilePath, CAPTURED_FILETYPE eFileType)
                      {
                      FileLog *ObjFileLog = NULL;
                      ObjFileLog = new FileLog();
                      if(IS_VALID_OBJ(ObjFileLog))
                      {
                      ObjFileLog->sFilePath = sFilePath;
                      ObjFileLog->u8FileType = eFileType;
                      ObjFileLog->dCurrentDate = QDate::currentDate();
                      ObjFileLog->u8Dirty = DIRTYBIT_SET;

                          ObjFileLog->InsertData(u32CamFileId);
                          DELETE_OBJ(ObjFileLog);
                      }
                      

                      }


                      now we have used this u32CamFileId to insert in readinglog table

                      ObjReadingLog->u32FileId = u32CamFileId;


                      why above code return -1 for u32CamFileId ?

                      jsulmJ Online
                      jsulmJ Online
                      jsulm
                      Lifetime Qt Champion
                      wrote on last edited by
                      #10

                      @Qt-embedded-developer said in What is problem with qQuery.lastInsertId().toInt() ? why below function return -1 for u32FileId variable ?:

                      why above code return -1 for u32CamFileId ?

                      If you do not answer my questions I cannot help you...

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

                      Q 1 Reply Last reply
                      1
                      • jsulmJ jsulm

                        @Qt-embedded-developer said in What is problem with qQuery.lastInsertId().toInt() ? why below function return -1 for u32FileId variable ?:

                        why above code return -1 for u32CamFileId ?

                        If you do not answer my questions I cannot help you...

                        Q Offline
                        Q Offline
                        Qt embedded developer
                        wrote on last edited by
                        #11

                        @jsulm

                        no new row inserted in file log

                        yes we get auto increment id where -1 not returned

                        why above code return -1 for u32CamFileId ? this questions answer i not get it. why it comes i want to know

                        jsulmJ 1 Reply Last reply
                        0
                        • Q Qt embedded developer

                          @jsulm

                          no new row inserted in file log

                          yes we get auto increment id where -1 not returned

                          why above code return -1 for u32CamFileId ? this questions answer i not get it. why it comes i want to know

                          jsulmJ Online
                          jsulmJ Online
                          jsulm
                          Lifetime Qt Champion
                          wrote on last edited by
                          #12

                          @Qt-embedded-developer said in What is problem with qQuery.lastInsertId().toInt() ? why below function return -1 for u32FileId variable ?:

                          why above code return -1 for u32CamFileId ?

                          I guess because nothing was inserted?
                          "no new row inserted in file log"

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

                          Q 1 Reply Last reply
                          3
                          • jsulmJ jsulm

                            @Qt-embedded-developer said in What is problem with qQuery.lastInsertId().toInt() ? why below function return -1 for u32FileId variable ?:

                            why above code return -1 for u32CamFileId ?

                            I guess because nothing was inserted?
                            "no new row inserted in file log"

                            Q Offline
                            Q Offline
                            Qt embedded developer
                            wrote on last edited by
                            #13

                            @jsulm thank you

                            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