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
    Jul 2012
    Posts
    53
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows
    Thanks
    16
    Thanked 1 Time in 1 Post

    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 

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

    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

  3. #3
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    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.

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

    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?

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

    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().

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

    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 

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

    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 ?

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

    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.

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

    Default Re: qsqlquery UPDATE how to?

    Where did you get the value of rowid?

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

    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.

  11. #11
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    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.

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

    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.

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.