I realise this is an old question but in Qt 5.10 (and likely earlier) there is a function QSqlQuery::lastInsertId()
which can be called after QSqlQuery::exec()
.
It's quite useful if you are using a database such as SQLite which doesn't support the RETURNING
clause on an INSERT
statement.
QSqlQuery::lastInsertId() documentation.
Usage is something along the lines of the following:
QSqlQuery q;
q.prepare("INSERT INTO table_name VALUES(:some_column_name)");
q.bindValue(":some_column_name", "FooBar");
q.exec();
qDebug() << "Last ID was:" << q.lastInsertId();