PDA

View Full Version : Form update problem



thefatladysingsopera
12th September 2011, 11:08
I am trying to update my form with the following code by the form won't save the edits.


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.cityEdit->text());
query.bindValue(":id", recordz.value("id").toInt());
query.exec();
tableModel->submitAll();

wysota
12th September 2011, 13:54
Your code doesn't make any sense. Replace line #13 with tableModel->select(). Of course you will lose all the changes done through the model (if any). Better yet, use QDataWidgetMapper.

thefatladysingsopera
12th September 2011, 14:51
I am already using QDataWidgetMapper to navigate between records.I am writing a simple sqlite manager and this is the first time i have written this much code.I will look into my saveEdits function afresh.Its the only function that remains.

wysota
12th September 2011, 15:03
I am already using QDataWidgetMapper to navigate between records.
Use it to edit the records.

thefatladysingsopera
12th September 2011, 15:21
I don't have a permanent table set in my model so i am choosing what table i want in my model.

wysota
12th September 2011, 15:28
I don't see how it is related to the problem.

thefatladysingsopera
12th September 2011, 17:28
Use it to edit the records.

I have tried it and works to some degree but the problem is that it inserts duplicate records on other tables.

Unrelated: QSqlTableModel has this function tableName () that returns the name of the currently selected table.In my application,i have my own way of getting the currently selected table but even so,if you are going to be editing the records using QDataWidget mapper in such an arrangement,i reckon its going to be too complicated.

norobro
12th September 2011, 17:53
Is that extra closing parenthesis in your query after "id=:id" a typo?

wysota
13th September 2011, 08:42
I have tried it and works to some degree but the problem is that it inserts duplicate records on other tables.
I think it is the problem of your code and not anything inherent to QDataWidgetMapper.


Unrelated: QSqlTableModel has this function tableName () that returns the name of the currently selected table.In my application,i have my own way of getting the currently selected table but even so,if you are going to be editing the records using QDataWidget mapper in such an arrangement,i reckon its going to be too complicated.
I really don't see how table names are related to QDataWidgetMapper. The latter works on generic models, it doesn't care about any table names.

thefatladysingsopera
13th September 2011, 08:56
Thanks so much for your comments.My code i admit is really not that good and in the coming days i will be taking more Qt classes to improve my skills.