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

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.