开发者

Updating a record in QSqlTableModel

I am trying to update a record and i have this:

tableModel->select();
QModelIndex index = ui.tableView->currentIndex();
QString sqlQuery = QString("UPDATE %1 SET firstname=:firstname, lastname=:lastname,  country=:country, city=:city WHERE id=:id)").arg(tableName);
query.prepare(sqlQuery);
QSqlRecord recordz = tableModel->record(index.row());

query.bindValue(":firstname", ui.fEdit->text());
query.bindValue(":lastname",  ui.lnEdit->text());
query.bindValue(":country", ui.cEdit->text());
query.bindValue(":city",  ui开发者_StackOverflow.cityEdit->text());
query.bindValue(":id", recordz.value("id").toInt());
query.exec();
tableModel->submitAll();

The application compiles without errors but it won't save any edits.


    query.bindValue(":id", ui.tableView->currentIndex());

There's your offending line of code. You can use the data functions to try return the actual index or value, but remember your tableView index != your SQL database index. You ever drop a row your index on database will be different to your index in Qt, so you'll need to include the actual DB ID into your initial SQL queries and keep it stored alongside the other values, and then return it when you run your update query.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜