Results 1 to 4 of 4

Thread: QSqlQuery: Getting an Auto Increment value

  1. #1
    Join Date
    Dec 2009
    Posts
    47
    Thanks
    11
    Thanked 1 Time in 1 Post
    Qt products
    Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android

    Default QSqlQuery: Getting an Auto Increment value

    Hi

    How can I get the row id of a record added by QSqlQuery::exec("INSERT ...") if it is automatically assigned by the database?

    There is an old thread on a closely related topic (see http://www.qtcentre.org/forum/f-qt-p...mit-12908.html), but the proposed solutions did not work for me:

    • lastInsertedId(): not supported by database or driver (Microsoft Access, QODBC)
    • bind NULL, i.e., QVariant(QVariant::Int) with QSql::ParamType InOut: QSqlQuery::exec fails with the message "you tried to assign the Null value to a variable that is not a Variant data type" (yes, I tried; how else can I bind a Null value if not as variant?)
    • bind NULL, i.e., QVariant(QVariant::Int) with QSql::ParamType Out: QSqlQuery::exec fails with the message "Optional feature not implemented"


    My current workaround is very ugly: I insert the record with a modified field (I add 100 to an int field that is in reality maximally 10), then SELECT .... WHERE value > 100, store the row id, then UPDATE with the corrected value).

    Any less cumbersome method is highly appreciated.

    Al_

  2. #2
    Join Date
    Dec 2009
    Location
    Brisbane
    Posts
    17
    Thanked 3 Times in 3 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QSqlQuery: Getting an Auto Increment value

    You're right, odbc doesn't support retrieval of this value, but a bit of googling turned up that apparently access supports "select @@identity", which apparently gives the information you want.

    Hope this helps.

  3. The following user says thank you to tangential for this useful post:

    Al_ (23rd December 2009)

  4. #3
    Join Date
    Dec 2009
    Posts
    47
    Thanks
    11
    Thanked 1 Time in 1 Post
    Qt products
    Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android

    Smile Re: QSqlQuery: Getting an Auto Increment value

    YES! Works perfectly, many thanks. I feel a bit ashamed that I did not find this statement myself; but just little, judged by the many threads to this topic without solution, @@identity seems to be little known.

  5. #4
    Join Date
    Dec 2009
    Location
    Brisbane
    Posts
    17
    Thanked 3 Times in 3 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QSqlQuery: Getting an Auto Increment value

    Yeah, I found it about 3 levels deep when I was looking to see if there was a way to retrieve the information via ODBC (so I could fix the qt driver). Glad to have been able to help

Similar Threads

  1. Replies: 1
    Last Post: 20th May 2009, 20:36
  2. Replies: 4
    Last Post: 9th May 2008, 17:02
  3. Problems with QSqlQuery update
    By whoops.slo in forum Qt Programming
    Replies: 4
    Last Post: 28th August 2006, 07:17

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.