that_guy
that_guy

Reputation: 13

QSqlQuery.record() is always empty

I'm editing the database in this manner:

QSqlQuery query;

query.prepare("UPDATE student "
        "SET name = ? "
        "WHERE id = ?");

QString name = "t"; 
int id = 3;

query.addBindValue(name);
query.addBindValue(id);

query.exec(); // query exec returns true

QSqlRecord record = query.record(); // but the record is empty!
mTableModel->beforeInsert(record); 

The retrieved record is always empty, but the QSqlTableModel still changes! I need the record to be valid because I'm trying to synchronize an sql db with a std::vector.

I'm connecting to the database like this:

mDatabase = QSqlDatabase::addDatabase("QSQLITE");
mDatabase.setDatabaseName("database.db");
mDatabase.open();

I tried calling QSqlQuery::clear(), QSqlQuery::finish() but it didn't help. I also tried to open and close the DB, but it also didn't help. What can I do? :\

Upvotes: 1

Views: 2771

Answers (1)

epsilon
epsilon

Reputation: 2969

Qt is not a pain indeed.

All your code is good. The only wrong assumption is that an update request will automatically give you back the updated record. You have to make a new select request on this id to get the updates data in a QSqlRecord.

//[untested]
QSqlQuery select;
select.prepare("SELECT * from student where id = ?");
select.addBindValue(id);

if (select.exec() && select.next()) {
  QSqlRecord record = select.record();
}

Upvotes: 1

Related Questions