PDA

View Full Version : MYSQL save changes



eleanor
7th October 2007, 10:24
Hi. I'm using MySQL to write/read from the Mysql database.

I'm using QTableView to represent tables on the screen.
The problem is when the table is shown, and if I edit it manually, then I want to save the changes and I don't know how.




The second problem is:


void dbWindow::add_changes_kraj() {
QSqlQuery query;
QString table_name = "kraj";
//we use NULL because this is not php, and it will auto increment on it's own
query.prepare("INSERT INTO :table (id,ime,postna_st) VALUES(NULL,:ime,:postna_st)");
query.bindValue(":table",table_name);
query.bindValue(":ime",edit_ime->text());
query.bindValue(":postna_st",edit_postna_st->text());
query.exec();
//query.exec("insert into kraj values(NULL,'Town','4532')");
emit close();
}

- what is commented (query.exec("insert into kraj values(NULL,'Postojna','4532')");) works perfectly, but the other lines don't (which is the same...and I need to make them work).

Any idea?

jpn
7th October 2007, 12:08
What happens if you don't try to bind the table name? Also, QSqlQuery::bindValue() docs seems to suggest binding NULL values like this:


query.prepare("INSERT INTO kraj (id,ime,postna_st) VALUES (:id,:ime,:postna_st)");
query.bindValue(":id",QVariant(QVariant::Int));
query.bindValue(":ime",edit_ime->text());
query.bindValue(":postna_st",edit_postna_st->text());
query.exec();

eleanor
7th October 2007, 13:22
It works now. Thank you.

Now I only need a way to store the changed values into mysql table (I'm changing in QTableView).

jpn
7th October 2007, 13:27
You have to decide in which way you want your application to behave (see QSqlTableModel::EditStrategy (http://doc.trolltech.com/latest/qsqltablemodel.html#EditStrategy-enum)). In case you decide to use QSqlTableModel::OnManualSubmit, you will have to use QSqlTableModel::submitAll() to submit changes. Otherwise changes will get automatically submitted as QSqlTableModel::EditStrategy (http://doc.trolltech.com/latest/qsqltablemodel.html#EditStrategy-enum) docs explain.