Results 1 to 17 of 17

Thread: qsqlquery UPDATE how to?

  1. #1
    Join Date
    Mar 2009
    Posts
    104
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    MacOS X

    Default qsqlquery UPDATE how to?

    I read all the documentation of QSqlQuery and all topics in the net.Unfortunately there are examples only with INSERT or SELECT statement.
    I have the code below working:
    Qt Code:
    1. QSqlQuery query;
    2. query.prepare("INSERT INTO clients (ClientName, ClientCity, ClientAddress, ClientMol, ClientEik, ClientVat, ClientTel, ClientMail)"
    3. "VALUES (?, ?, ?, ?, ?, ?, ?, ?)");
    4. query.addBindValue(ui->lineEdit_name->text());
    5. query.addBindValue(ui->lineEdit_city->text());
    6. query.addBindValue(ui->lineEdit_address->text());
    7. query.addBindValue(ui->lineEdit_mol->text());
    8. query.addBindValue(ui->lineEdit_eik->text());
    9. query.addBindValue(ui->lineEdit_vat->text());
    10. query.addBindValue(ui->lineEdit_tel->text());
    11. query.addBindValue(ui->lineEdit_mail->text());
    12. query.exec();
    To copy to clipboard, switch view to plain text mode 

    What i need is the same but with UPDATE and as condition WHERE ROWID=rowid
    I don't know where to put this WHERE and where the value of rowid.I have tried a lot.

  2. #2
    Join Date
    Jul 2012
    Posts
    53
    Thanks
    16
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: qsqlquery UPDATE how to?

    Just use
    Qt Code:
    1. query.prepare("UPDATE ...");
    To copy to clipboard, switch view to plain text mode 
    with
    Qt Code:
    1. query.bindValue();
    To copy to clipboard, switch view to plain text mode 
    Or use
    Qt Code:
    1. QSqlTableModel::SetData();
    To copy to clipboard, switch view to plain text mode 
    This is written in "C++ GUI Programming with Qt 4".

  3. #3
    Join Date
    Mar 2009
    Posts
    104
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    MacOS X

    Default Re: qsqlquery UPDATE how to?

    I tried with this:

    Qt Code:
    1. QSqlQuery query;
    2. query.prepare("UPDATE SET clients ClientName=:name, ClientCity=:city, ClientAddress=:address, ClientMol=:mol, ClientEik=:eik, ClientVat=:vat, ClientTel=:tel, ClientMail=:mail WHERE ROWID=:rowid");
    3. query.bindValue(":name", ui->lineEdit_name->text());
    4. query.bindValue(":city", ui->lineEdit_city->text());
    5. query.bindValue(":address", ui->lineEdit_address->text());
    6. query.bindValue(":mol", ui->lineEdit_mol->text());
    7. query.bindValue(":eik", ui->lineEdit_eik->text());
    8. query.bindValue(":vat", ui->lineEdit_vat->text());
    9. query.bindValue(":tel", ui->lineEdit_tel->text());
    10. query.bindValue(":mail", ui->lineEdit_mail->text());
    11. query.bindValue(":rowid", QString::number(rowid));
    12. query.exec();
    To copy to clipboard, switch view to plain text mode 

    Still doesn't work.query.lastError() returns Parameter count mismatch
    Last edited by unix7777; 29th August 2012 at 21:30.

  4. #4
    Join Date
    Jul 2012
    Posts
    53
    Thanks
    16
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: qsqlquery UPDATE how to?

    The grammar looks right. Maybe you should try to add your database to the query declaration so the query knows which is the default database?

    Qt Code:
    1. QSqlQuery query(db);
    To copy to clipboard, switch view to plain text mode 

  5. #5
    Join Date
    Apr 2012
    Location
    Bali - Indonesia
    Posts
    9
    Thanks
    2
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: qsqlquery UPDATE how to?

    Hi Unix777,

    I make a sample using postgresql, hope this can help you
    Qt Code:
    1. QString user = ui->lineEdit_user->text();
    2. QString pass = ui->lineEdit_pass->text();
    3.  
    4.  
    5. if (!db.isOpen())
    6. db.open();
    7.  
    8. QSqlQuery query(QString("UPDATE mnp_userlist SET (userlist_pass) = (\'%0\') WHERE userlist_user = \'%1\';").arg(pass).arg(user), db);
    9. db.close();
    To copy to clipboard, switch view to plain text mode 

    Regards,
    Mardi

  6. #6
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: qsqlquery UPDATE how to?

    As Mardi's example shows,
    Qt Code:
    1. query.prepare("UPDATE SET clients ClientName=:name, ClientCity=:city, ClientAddress=:address, ClientMol=:mol, ClientEik=:eik, ClientVat=:vat, ClientTel=:tel, ClientMail=:mail WHERE ROWID=:rowid");
    To copy to clipboard, switch view to plain text mode 
    is not a valid SQL statement, it should read :
    Qt Code:
    1. UPDATE clients SET ...
    To copy to clipboard, switch view to plain text mode 

    Do NOT build SQL by pasting strings as Mardi suggests. Using bindValue() is, by far, the better and safer option.

  7. #7
    Join Date
    Mar 2009
    Posts
    104
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    MacOS X

    Default Re: qsqlquery UPDATE how to?

    At first thank you to both of you!

    I've changed the code to:
    Qt Code:
    1. QSqlQuery query;
    2. query.prepare("UPDATE clients SET ClientName=:name, ClientCity=:city, ClientAddress=:address, ClientMol=:mol, ClientEik=:eik, ClientVat=:vat, ClientTel=:tel, ClientMail=:mail WHERE ROWID=:rowid");
    3. query.bindValue(":name", ui->lineEdit_name->text());
    4. query.bindValue(":city", ui->lineEdit_city->text());
    5. query.bindValue(":address", ui->lineEdit_address->text());
    6. query.bindValue(":mol", ui->lineEdit_mol->text());
    7. query.bindValue(":eik", ui->lineEdit_eik->text());
    8. query.bindValue(":vat", ui->lineEdit_vat->text());
    9. query.bindValue(":tel", ui->lineEdit_tel->text());
    10. query.bindValue(":mail", ui->lineEdit_mail->text());
    11. query.bindValue(":rowid", QString::number(rowid));
    12. query.exec();
    To copy to clipboard, switch view to plain text mode 

    But still nothing happens.The db is declared in main function and this is not the problem, because INSERT function works on same way!
    Another suggestions?

  8. #8
    Join Date
    Mar 2008
    Location
    Kraków, Poland
    Posts
    1,536
    Thanked 284 Times in 279 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: qsqlquery UPDATE how to?

    Because the glass ball is now free :
    1. Why are binding to :rowid text value not number ? What is a type of rowid column in database ?
    2. What is a result of query.exec() (true or false).
    3. If false what is a result of query.lastError().

  9. #9
    Join Date
    Mar 2009
    Posts
    104
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    MacOS X

    Default Re: qsqlquery UPDATE how to?

    With this code i got "Success!" but nothing happens again!
    In accordance with sqlite documentation ROWID should be a int.
    Qt Code:
    1. QSqlQuery query;
    2. query.prepare("UPDATE clients SET ClientName=:name, ClientCity=:city, ClientAddress=:address, ClientMol=:mol, ClientEik=:eik, ClientVat=:vat, ClientTel=:tel, ClientMail=:mail WHERE ROWID=:rowid");
    3. query.bindValue(":name", ui->lineEdit_name->text());
    4. query.bindValue(":city", ui->lineEdit_city->text());
    5. query.bindValue(":address", ui->lineEdit_address->text());
    6. query.bindValue(":mol", ui->lineEdit_mol->text());
    7. query.bindValue(":eik", ui->lineEdit_eik->text());
    8. query.bindValue(":vat", ui->lineEdit_vat->text());
    9. query.bindValue(":tel", ui->lineEdit_tel->text());
    10. query.bindValue(":mail", ui->lineEdit_mail->text());
    11. query.bindValue(":rowid", rowid);
    12. if (query.exec())
    13. {
    14. emit updateTable();
    15. QMessageBox::information(this,"", "Success!");
    16. }
    17. else
    18. {
    19. QMessageBox::information(this,"", query.lastError().text());
    20.  
    21. }
    22. this->close();
    To copy to clipboard, switch view to plain text mode 

  10. #10
    Join Date
    Mar 2008
    Location
    Kraków, Poland
    Posts
    1,536
    Thanked 284 Times in 279 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: qsqlquery UPDATE how to?

    It just means that the SQL statement is correct formally. What is the value of the variable rowid ? There has to be a record in the table with the corresponding rowid ?

  11. #11
    Join Date
    Mar 2009
    Posts
    104
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    MacOS X

    Default Re: qsqlquery UPDATE how to?

    The SQLite editor i use SQLiteStudio doesn't show ROWID. In the program row id is an int.But in another parts i use QString to insert it in the query.

  12. #12
    Join Date
    Mar 2008
    Location
    Kraków, Poland
    Posts
    1,536
    Thanked 284 Times in 279 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: qsqlquery UPDATE how to?

    Where did you get the value of rowid?

  13. #13
    Join Date
    Mar 2009
    Posts
    104
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    MacOS X

    Default Re: qsqlquery UPDATE how to?

    there is a member function that set it.And i pass it after creating the object.The rowid int works i test it with QMessage, it display exactly what i select.

  14. #14
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: qsqlquery UPDATE how to?

    If "nothing is happening" then either;
    • the rowid is does not correspond to any row in the table, or
    • the row already contained the data you were updating it to and therefore has no net change.


    ROWID is an internal row identifier provided by Sqlite. It is a 64-bit integer allocated by Sqlite (not you). The only reasonable source for a rowid is from an existing row in the same Sqlite table.

  15. #15
    Join Date
    Mar 2009
    Posts
    104
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    MacOS X

    Default Re: qsqlquery UPDATE how to?

    I have changed everything.In accordance with documentation here http://stackoverflow.com/questions/5...em-with-python
    i add id int and i give NULL for every record.On this way it auto-increment every next record.What happens - everything work except the UPDATE statement.
    It gives me: " Parameter count mismatch"!

    Qt Code:
    1. QString row=QString::number(rowid);
    2. QSqlQuery query;
    3. query.prepare("UPDATE clients SET (ClientName, ClientCity, ClientAddress, ClientMol, ClientEik, ClientVat, ClientTel, ClientMail) WHERE id="+row+
    4. "VALUES (?, ?, ?, ?, ?, ?, ?, ?)");
    5. query.addBindValue(ui->lineEdit_name->text());
    6. query.addBindValue(ui->lineEdit_city->text());
    7. query.addBindValue(ui->lineEdit_address->text());
    8. query.addBindValue(ui->lineEdit_mol->text());
    9. query.addBindValue(ui->lineEdit_eik->text());
    10. query.addBindValue(ui->lineEdit_vat->text());
    11. query.addBindValue(ui->lineEdit_tel->text());
    12. query.addBindValue(ui->lineEdit_mail->text());
    13.  
    14. this->close();
    15. if (query.exec())
    16. {
    17. emit updateTable();
    18. }
    19. else
    20. {
    21. QMessageBox::information(this,"", query.lastError().text());
    22. }
    23.  
    24. this->close();
    To copy to clipboard, switch view to plain text mode 

    I would like to test it with some exact number of row, for example i want to update row 1 only to see whether it works.Unfortunately i don't know the syntax to do it.

  16. #16
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: qsqlquery UPDATE how to?

    You have had the correct syntax for at least the past few posts. I have no idea why you are still thrashing around with this.

    Here is a canned example that updates the columns of a row:
    Qt Code:
    1. #include <QtCore>
    2. #include <QtSql>
    3. #include <QDebug>
    4.  
    5. int main(int argc, char *argv[])
    6. {
    7. QCoreApplication app(argc, argv);
    8.  
    9. QSqlDatabase db = QSqlDatabase::addDatabase("QSQLITE");
    10. db.setDatabaseName("test.db");
    11. if (db.open()) {
    12. QSqlQuery query;
    13.  
    14. // Create a test table
    15. query.exec(
    16. "create table clients ("
    17. "id int, "
    18. "ClientName VARCHAR(10),"
    19. "ClientCity VARCHAR(10),"
    20. "ClientAddress VARCHAR(10),"
    21. "ClientMol VARCHAR(10),"
    22. "ClientEik VARCHAR(10),"
    23. "ClientVat VARCHAR(10),"
    24. "ClientTel VARCHAR(10),"
    25. "ClientMail VARCHAR(10) )" );
    26. query.exec("insert into clients values(1, 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H')" );
    27. query.exec("insert into clients values(2, 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H')" );
    28.  
    29. // Your Code
    30. query.prepare("UPDATE clients SET "
    31. "ClientName=:name, ClientCity=:city, ClientAddress=:address, ClientMol=:mol, "
    32. "ClientEik=:eik, ClientVat=:vat, ClientTel=:tel, ClientMail=:mail WHERE id=:id");
    33. query.bindValue(":name", "Z");
    34. query.bindValue(":city", "Z") ;
    35. query.bindValue(":address", "Z");
    36. query.bindValue(":mol", "Z");
    37. query.bindValue(":eik", "Z");
    38. query.bindValue(":vat", "Z");
    39. query.bindValue(":tel", "Z");
    40. query.bindValue(":mail", "Z");
    41. query.bindValue(":id", 1);
    42. if (query.exec())
    43. qDebug() << "Done OK";
    44. else
    45. qDebug() << "Huh!";
    46.  
    47. // Dump the table id and ClientName
    48. query.exec("SELECT * FROM clients");
    49. while (query.next()) {
    50. qDebug() << query.value(0).toInt() << query.value(1).toString();
    51. }
    52. }
    53.  
    54.  
    55. return 0;
    56. }
    To copy to clipboard, switch view to plain text mode 

  17. #17
    Join Date
    Mar 2009
    Posts
    104
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    MacOS X

    Default Re: qsqlquery UPDATE how to?

    Now everything works!
    THANKS TO ALL OF YOU!
    Qt Code:
    1. QSqlQuery query;
    2. query.prepare("UPDATE clients SET ClientName=:name, ClientCity=:city, ClientAddress=:address, ClientMol=:mol, ClientEik=:eik, ClientVat=:vat, ClientTel=:tel, ClientMail=:mail WHERE id=:id ");
    3. query.bindValue(":name", ui->lineEdit_name->text());
    4. query.bindValue(":city", ui->lineEdit_city->text());
    5. query.bindValue(":address", ui->lineEdit_address->text());
    6. query.bindValue(":mol", ui->lineEdit_mol->text());
    7. query.bindValue(":eik", ui->lineEdit_eik->text());
    8. query.bindValue(":vat", ui->lineEdit_vat->text());
    9. query.bindValue(":tel", ui->lineEdit_tel->text());
    10. query.bindValue(":mail", ui->lineEdit_mail->text());
    11. query.bindValue(":id", clientid);
    12. QMessageBox::information(this,"", clientid);
    13. this->close();
    14. if (query.exec())
    15. {
    16. emit updateTable();
    17. }
    18. else
    19. {
    20. QMessageBox::information(this,"", query.lastError().text());
    21. }
    22.  
    23. this->close();
    To copy to clipboard, switch view to plain text mode 

Similar Threads

  1. QFileSystemModel - Incremental update/pre-emptive update
    By johnnyturbo3 in forum Qt Programming
    Replies: 0
    Last Post: 2nd September 2011, 13:56
  2. Replies: 1
    Last Post: 18th July 2011, 12:12
  3. Replies: 2
    Last Post: 29th September 2010, 17:44
  4. Batch update fails with QSqlQuery
    By William Wilson in forum Qt Programming
    Replies: 2
    Last Post: 20th July 2007, 15:36
  5. Problems with QSqlQuery update
    By whoops.slo in forum Qt Programming
    Replies: 4
    Last Post: 28th August 2006, 07:17

Tags for this Thread

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.