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. Using lastInsertId() functionality with QSqlTableModel
Forum Update on Monday, May 27th 2025

Using lastInsertId() functionality with QSqlTableModel

Scheduled Pinned Locked Moved General and Desktop
3 Posts 2 Posters 5.3k 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.
  • L Offline
    L Offline
    LizMM
    wrote on 28 Mar 2011, 16:24 last edited by
    #1

    Hello,

    I have a MYSQL database that I am interfacing to using the QSqlTableModel class. This is working out great except for one thing: I dont seem to have the functionality that the simpler QSqlQuery class has and that is the lastInsertId() function.

    The lastInsertId function is really useful since I can get back my ID for a row I just inserted to the database.

    Does anyone know of a way to get the last inserted ID using the QSqlTableModel?

    Thanks,
    Liz

    1 Reply Last reply
    0
    • Z Offline
      Z Offline
      ZapB
      wrote on 28 Mar 2011, 17:26 last edited by
      #2

      I think that QSqlTableMOdel inherits form QSqlQueryModel which provides access to the QSqlQUery via the query() function. You should be able to call the lastInsertId function on the returned QSqlQuery object.

      Nokia Certified Qt Specialist
      Interested in hearing about Qt related work

      1 Reply Last reply
      0
      • L Offline
        L Offline
        LizMM
        wrote on 28 Mar 2011, 18:36 last edited by
        #3

        Yes that works. Thanks!

        1 Reply Last reply
        0

        1/3

        28 Mar 2011, 16:24

        • 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