Results 1 to 4 of 4

Thread: MYSQL save changes

  1. #1
    Join Date
    Nov 2006
    Posts
    96

    Default MYSQL save changes

    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:

    Qt Code:
    1. void dbWindow::add_changes_kraj() {
    2. QSqlQuery query;
    3. QString table_name = "kraj";
    4. //we use NULL because this is not php, and it will auto increment on it's own
    5. query.prepare("INSERT INTO :table (id,ime,postna_st) VALUES(NULL,:ime,:postna_st)");
    6. query.bindValue(":table",table_name);
    7. query.bindValue(":ime",edit_ime->text());
    8. query.bindValue(":postna_st",edit_postna_st->text());
    9. query.exec();
    10. //query.exec("insert into kraj values(NULL,'Town','4532')");
    11. emit close();
    12. }
    To copy to clipboard, switch view to plain text mode 

    - 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?
    Last edited by eleanor; 7th October 2007 at 11:31.

  2. #2
    Join Date
    Feb 2006
    Location
    Oslo, Norway
    Posts
    6,264
    Thanks
    36
    Thanked 1,519 Times in 1,389 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows Symbian S60 Maemo/MeeGo

    Default Re: MYSQL save changes

    What happens if you don't try to bind the table name? Also, QSqlQuery::bindValue() docs seems to suggest binding NULL values like this:
    Qt Code:
    1. query.prepare("INSERT INTO kraj (id,ime,postna_st) VALUES (:id,:ime,:postna_st)");
    2. query.bindValue(":id",QVariant(QVariant::Int));
    3. query.bindValue(":ime",edit_ime->text());
    4. query.bindValue(":postna_st",edit_postna_st->text());
    5. query.exec();
    To copy to clipboard, switch view to plain text mode 
    J-P Nurmi

  3. #3
    Join Date
    Nov 2006
    Posts
    96

    Default Re: MYSQL save changes

    It works now. Thank you.

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

  4. #4
    Join Date
    Feb 2006
    Location
    Oslo, Norway
    Posts
    6,264
    Thanks
    36
    Thanked 1,519 Times in 1,389 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows Symbian S60 Maemo/MeeGo

    Default Re: MYSQL save changes

    You have to decide in which way you want your application to behave (see QSqlTableModel::EditStrategy). 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 docs explain.
    J-P Nurmi

Similar Threads

  1. MySQL starting problem
    By shamik in forum General Discussion
    Replies: 5
    Last Post: 25th April 2007, 08:20
  2. Qt 4.1.4 & Mysql 5 on Linux x64
    By bothapn in forum Installation and Deployment
    Replies: 7
    Last Post: 4th August 2006, 14:23
  3. What does "Save All" actually save?
    By Mariane in forum Newbie
    Replies: 7
    Last Post: 31st January 2006, 14:23

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.