Results 1 to 17 of 17

Thread: qsqlquery UPDATE how to?

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Join Date
    Mar 2008
    Location
    Kraków, Poland
    Posts
    1,540
    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().

  2. #2
    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 

  3. #3
    Join Date
    Mar 2008
    Location
    Kraków, Poland
    Posts
    1,540
    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 ?

  4. #4
    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.

  5. #5
    Join Date
    Mar 2008
    Location
    Kraków, Poland
    Posts
    1,540
    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?

  6. #6
    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.

  7. #7
    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.

  8. #8
    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.

  9. #9
    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 

  10. #10
    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
  •  
Qt is a trademark of The Qt Company.