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, stored procedure, lastInsertId
Forum Updated to NodeBB v4.3 + New Features

QSqlQuery, stored procedure, lastInsertId

Scheduled Pinned Locked Moved Unsolved General and Desktop
3 Posts 2 Posters 301 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.
  • S Offline
    S Offline
    SPlatten
    wrote on 13 Oct 2021, 12:08 last edited by
    #1

    I have a stored procedure that inserts a record to the database.

    QSqlQuery query;
    query.prepare("CALL addThreat(?,?,?);");
    query.addBindValue(muintSelectedSDid);
    query.addBindValue(bytaryJSON);
    query.addBindValue(QVariant::String);
    if ( Trainer::queryDB(query, false, &uintNewThreatID) == true ) {
    }
    

    The third parameter is optional and defaults to nullptr if not supplied. Internally the query executes the stored procedure and this works, but also internal to the function queryDB, it calls:

    QVariant varInsertId(rQuery.lastInsertId());
    *puintInsertID = varInsertId.toUInt();
    

    puintInsertID is assigned 0. Is this a limitation, can I get the insert ID from the insert operation in the stored procedure?

    Kind Regards,
    Sy

    J 1 Reply Last reply 13 Oct 2021, 13:43
    0
    • S SPlatten
      13 Oct 2021, 12:08

      I have a stored procedure that inserts a record to the database.

      QSqlQuery query;
      query.prepare("CALL addThreat(?,?,?);");
      query.addBindValue(muintSelectedSDid);
      query.addBindValue(bytaryJSON);
      query.addBindValue(QVariant::String);
      if ( Trainer::queryDB(query, false, &uintNewThreatID) == true ) {
      }
      

      The third parameter is optional and defaults to nullptr if not supplied. Internally the query executes the stored procedure and this works, but also internal to the function queryDB, it calls:

      QVariant varInsertId(rQuery.lastInsertId());
      *puintInsertID = varInsertId.toUInt();
      

      puintInsertID is assigned 0. Is this a limitation, can I get the insert ID from the insert operation in the stored procedure?

      J Offline
      J Offline
      JonB
      wrote on 13 Oct 2021, 13:43 last edited by
      #2

      @SPlatten
      Hope the following helps, not sure but...

      I don't know about rQuery.lastInsertId().

      If you can program the stored procedure there is a SQL variable (depends on which SQL) something like @LAST_INSERT_ID which you can access after the INSERT.

      You will have to play with how to get that back to your code. A while ago i vaguely remember someone here saying they could not get IN/OUT "VAR" parameters to work from the driver or Qt or whatever. If that is the case, I think I ended up telling him he had to do a SELECT @LAST_INSERT_ID in the SQL s.p. as the only way to be able to get it back to Qt SQL.

      S 1 Reply Last reply 13 Oct 2021, 13:48
      2
      • J JonB
        13 Oct 2021, 13:43

        @SPlatten
        Hope the following helps, not sure but...

        I don't know about rQuery.lastInsertId().

        If you can program the stored procedure there is a SQL variable (depends on which SQL) something like @LAST_INSERT_ID which you can access after the INSERT.

        You will have to play with how to get that back to your code. A while ago i vaguely remember someone here saying they could not get IN/OUT "VAR" parameters to work from the driver or Qt or whatever. If that is the case, I think I ended up telling him he had to do a SELECT @LAST_INSERT_ID in the SQL s.p. as the only way to be able to get it back to Qt SQL.

        S Offline
        S Offline
        SPlatten
        wrote on 13 Oct 2021, 13:48 last edited by
        #3

        @JonB , thank you, I have since changed the stored procedure to a function that returns the new ID.

        Kind Regards,
        Sy

        1 Reply Last reply
        0

        1/3

        13 Oct 2021, 12:08

        • Login

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