0

I am trying to obtain the ID (primary key with auto increment) of the last inserted data. But my query seems to be empty after using SCOPE_IDENTITY(), while the entries are inserted correctly into the database. What is causing the problem? Thank you!

QSqlQuery query;
query.prepare("INSERT INTO list (file) VALUES (?); SELECT SCOPE_IDENTITY()");
query.addBindValue(file);
qDebug() << query.exec();
qDebug() << query.first();
query.value(0).toInt();

Console Output:

true

false

QSqlQuery::value: not positioned on a valid record

Community
  • 1
  • 1
honiahaka10
  • 772
  • 4
  • 9
  • 29
  • Can't I use `SCOPE_IDENTITY()` in my query here? I found `QSqlQuery::lastInsertId()`, is it safe to use this function (if there are other queries executed in another thread)? – honiahaka10 Jan 03 '17 at 00:28
  • 3
    @honiahaka10 . . . `SCOPE_IDENTITY()` is part of SQL Server and you question is tagged MySQL. – Gordon Linoff Jan 03 '17 at 00:57

0 Answers0