Please nominate your Qt Champions for 2021!

Using lastInsertId() functionality with QSqlTableModel

  • 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?


  • 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.

  • Yes that works. Thanks!

Log in to reply