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. QODBC for MS Access doesn't support QSqlQuery::lastInsertId

QODBC for MS Access doesn't support QSqlQuery::lastInsertId

Scheduled Pinned Locked Moved Solved General and Desktop
10 Posts 4 Posters 767 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.
  • B Offline
    B Offline
    BrokenVoodooDoll
    wrote on last edited by
    #1

    I work with MS Access database (.accdb) and I need to insert some data and then get the last inserted ID. When I use QSqlQuery::lastInsertId method I see the following error:
    "QODBCResult::lastInsertId: not implemented for this DBMS" Error: ""
    I tried to use SELECT @@IDENTITY and similar expressions (SCOPE_IDENTITY and IDENTITY_CURRENT), but I get the same error.
    Some time ago I used SELECT @@IDENTITY in VBA and that worked. So it's not about MS Access, but the driver?
    Of course, I can write SELECT MAX(ID) FROM table, but I'm not sure it's safe.
    Is there a way to get the last inserted ID working with MS Access database?

    JonBJ 1 Reply Last reply
    0
    • Christian EhrlicherC Online
      Christian EhrlicherC Online
      Christian Ehrlicher
      Lifetime Qt Champion
      wrote on last edited by
      #2

      @BrokenVoodooDoll said in QODBC for MS Access doesn't support QSqlQuery::lastInsertId:

      So it's not about MS Access, but the driver?

      Yes, it's the odbc driver which does not support it

      Of course, I can write SELECT MAX(ID) FROM table, but I'm not sure it's safe.

      Correct, but if you're the only one working on this 'database' then it's ok

      Is there a way to get the last inserted ID working with MS Access database?

      only with the solution you posted above

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

      1 Reply Last reply
      1
      • B BrokenVoodooDoll

        I work with MS Access database (.accdb) and I need to insert some data and then get the last inserted ID. When I use QSqlQuery::lastInsertId method I see the following error:
        "QODBCResult::lastInsertId: not implemented for this DBMS" Error: ""
        I tried to use SELECT @@IDENTITY and similar expressions (SCOPE_IDENTITY and IDENTITY_CURRENT), but I get the same error.
        Some time ago I used SELECT @@IDENTITY in VBA and that worked. So it's not about MS Access, but the driver?
        Of course, I can write SELECT MAX(ID) FROM table, but I'm not sure it's safe.
        Is there a way to get the last inserted ID working with MS Access database?

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

        @BrokenVoodooDoll
        I would expect you to use SELECT @@IDENTITY as you said. You say "but I get the same error", yet the error message QODBCResult::lastInsertId is from QSqlQuery::lastInsertId so how can that relate to SELECT @@IDENTITY?

        1 Reply Last reply
        0
        • Kent-DorfmanK Offline
          Kent-DorfmanK Offline
          Kent-Dorfman
          wrote on last edited by
          #4

          ODBC has always about supporting the most common functionality of SQL in a platform independent manner. It is not surprising that it won't handle an M$ specific extention. Do it in the most transportable way, as is the intention of ODBC...and it will be safe if you follow transaction isolation rules.

          The dystopian literature that served as a warning in my youth has become an instruction manual in my elder years.

          1 Reply Last reply
          1
          • Christian EhrlicherC Online
            Christian EhrlicherC Online
            Christian Ehrlicher
            Lifetime Qt Champion
            wrote on last edited by Christian Ehrlicher
            #5

            @Kent-Dorfman said in QODBC for MS Access doesn't support QSqlQuery::lastInsertId:

            and it will be safe if you follow transaction isolation rules.

            No, it won't. It will only work if there is only one connection and if the query does not update insert more than one record.

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

            JonBJ Kent-DorfmanK 2 Replies Last reply
            1
            • Christian EhrlicherC Christian Ehrlicher

              @Kent-Dorfman said in QODBC for MS Access doesn't support QSqlQuery::lastInsertId:

              and it will be safe if you follow transaction isolation rules.

              No, it won't. It will only work if there is only one connection and if the query does not update insert more than one record.

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

              @Christian-Ehrlicher said in QODBC for MS Access doesn't support QSqlQuery::lastInsertId:

              if the query does not update more than one record

              update -> insert ?

              Christian EhrlicherC 1 Reply Last reply
              0
              • JonBJ JonB

                @Christian-Ehrlicher said in QODBC for MS Access doesn't support QSqlQuery::lastInsertId:

                if the query does not update more than one record

                update -> insert ?

                Christian EhrlicherC Online
                Christian EhrlicherC Online
                Christian Ehrlicher
                Lifetime Qt Champion
                wrote on last edited by
                #7

                @JonB Thx, fixed

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

                1 Reply Last reply
                1
                • Christian EhrlicherC Christian Ehrlicher

                  @Kent-Dorfman said in QODBC for MS Access doesn't support QSqlQuery::lastInsertId:

                  and it will be safe if you follow transaction isolation rules.

                  No, it won't. It will only work if there is only one connection and if the query does not update insert more than one record.

                  Kent-DorfmanK Offline
                  Kent-DorfmanK Offline
                  Kent-Dorfman
                  wrote on last edited by
                  #8

                  @Christian-Ehrlicher

                  Given the example, he's asking if the single SQL is atomic, and yes it is. Furthermore, if it exists within an isolated transaction block
                  BEGIN
                  SQL
                  SQL
                  SQL
                  END
                  then it's protected, depending upon the rules in effect for transaction isolation.

                  The dystopian literature that served as a warning in my youth has become an instruction manual in my elder years.

                  Christian EhrlicherC 1 Reply Last reply
                  0
                  • Kent-DorfmanK Kent-Dorfman

                    @Christian-Ehrlicher

                    Given the example, he's asking if the single SQL is atomic, and yes it is. Furthermore, if it exists within an isolated transaction block
                    BEGIN
                    SQL
                    SQL
                    SQL
                    END
                    then it's protected, depending upon the rules in effect for transaction isolation.

                    Christian EhrlicherC Online
                    Christian EhrlicherC Online
                    Christian Ehrlicher
                    Lifetime Qt Champion
                    wrote on last edited by
                    #9

                    @Kent-Dorfman No. If there is a stored procedure or something else which inserts something else somewhere it won't work.
                    I did not say that it does not work at all - I say that there a subtle cases where it does not work and therefore the recommendation to simply use it is plain wrong.

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

                    1 Reply Last reply
                    1
                    • B Offline
                      B Offline
                      BrokenVoodooDoll
                      wrote on last edited by
                      #10

                      Finally, it turned out, that a separate query with SELECT @@IDENTITY works (which is quite strange for me as I thought, that lastInserdId executes exactly the same query). I don't know how come I haven't noticed that. So one can do query.exec("INSERT ..."); and in the next line query.exec("SELECT @@IDENTITY");. But also I found out that SCOPE_IDENTITY and CURRENT_IDENTITY don't work. Thank you all for your help!

                      1 Reply Last reply
                      2

                      • Login

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