Results 1 to 11 of 11

Thread: sql query problem

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 sql query problem

    I want to update a record with this code.Unfortunately the record remains unchanged!
    Qt Code:
    1. QString name=ui->lineEdit_name->text();
    2. QString city=ui->lineEdit_city->text();
    3.  
    4. QSqlQuery query;
    5. query.prepare("UPDATE clients ClientName="+name+", ClientCity="+city+"WHERE RAWID="+rowid);
    6. query.exec();
    To copy to clipboard, switch view to plain text mode 

    What is the reason?Where i make mistake?

  2. #2
    Join Date
    Aug 2008
    Location
    Ukraine, Krivoy Rog
    Posts
    1,963
    Thanked 370 Times in 336 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: sql query problem

    This is how "prepare" _shouldn't_ work.
    This is how it _should_ work.
    PS. Where are you going to start reading docs carefully?
    Qt Assistant -- rocks!
    please, use tags [CODE] & [/CODE].

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

    Default Re: sql query problem

    Thanks.
    I saw the documentation before some time because i use this in another part of my code where i use SELECT statement.
    I changed the code to:
    Qt Code:
    1. QSqlQuery query;
    2. query.prepare("UPDATE clients (ClientName, ClientCity, ClientAddress, ClientTel, ClientMail, ClientMol, ClientEik, ClientVat) WHERE RAWID="+rowid+"VALUES (?, ?, ?, ?, ?, ?, ?, ?)");
    3. query.addBindValue(ui->lineEdit_name->text());
    4. query.addBindValue(ui->lineEdit_city->text());
    5. query.addBindValue(ui->lineEdit_address->text());
    6. query.addBindValue(ui->lineEdit_mol->text());
    7. query.addBindValue(ui->lineEdit_eik->text());
    8. query.addBindValue(ui->lineEdit_vat->text());
    9. query.addBindValue(ui->lineEdit_tel->text());
    10. query.addBindValue(ui->lineEdit_mail->text());
    11. query.exec();
    To copy to clipboard, switch view to plain text mode 
    and i still got some error of conversation probably because of +rowid+ i don't know

    i also changed it to:
    Qt Code:
    1. QSqlQuery query;
    2. query.prepare("UPDATE clients (ClientName=?, ClientCity=?, ClientAddress=?, ClientTel=?, ClientMail=?, ClientMol=?, ClientEik=?, ClientVat=?) WHERE RAWID="+rowid);
    3. query.addBindValue(ui->lineEdit_name->text());
    4. query.addBindValue(ui->lineEdit_city->text());
    5. query.addBindValue(ui->lineEdit_address->text());
    6. query.addBindValue(ui->lineEdit_tel->text());
    7. query.addBindValue(ui->lineEdit_mail->text());
    8. query.addBindValue(ui->lineEdit_mol->text());
    9. query.addBindValue(ui->lineEdit_eik->text());
    10. query.addBindValue(ui->lineEdit_vat->text());
    11. query.exec();
    To copy to clipboard, switch view to plain text mode 

    I HAVE TRIED ALSO:
    Qt Code:
    1. QSqlQuery query;
    2. query.prepare("UPDATE clients ClientName=?, ClientCity=?, ClientAddress=?, ClientTel=?, ClientMail=?, ClientMol=?, ClientEik=?, ClientVat=? WHERE RAWID=?");
    3. query.addBindValue(ui->lineEdit_name->text());
    4. query.addBindValue(ui->lineEdit_city->text());
    5. query.addBindValue(ui->lineEdit_address->text());
    6. query.addBindValue(ui->lineEdit_tel->text());
    7. query.addBindValue(ui->lineEdit_mail->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(QString::number(rowid));
    12. query.exec();
    13.  
    14. emit updateTable();
    15.  
    16. this->close();
    To copy to clipboard, switch view to plain text mode 

    Doesn't work again!
    P.S.I'm not lazy as you see.
    Last edited by unix7777; 25th August 2012 at 19:44.

  4. #4
    Join Date
    Oct 2011
    Location
    Germany
    Posts
    27
    Thanked 2 Times in 2 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: sql query problem

    Try this:

    Qt Code:
    1. QSqlQuery query;
    2. query.prepare("UPDATE clients ClientName=?, ClientCity=?, ClientAddress=?, ClientTel=?, ClientMail=?, ClientMol=?, ClientEik=?, ClientVat=? WHERE RAWID=?");
    3. query.addBindValue(0, ui->lineEdit_name->text());
    4. query.addBindValue(1, ui->lineEdit_city->text());
    5. query.addBindValue(2. ui->lineEdit_address->text());
    6. query.addBindValue(3, ui->lineEdit_tel->text());
    7. query.addBindValue(4, ui->lineEdit_mail->text());
    8. query.addBindValue(5, ui->lineEdit_mol->text());
    9. query.addBindValue(6, ui->lineEdit_eik->text());
    10. query.addBindValue(7, ui->lineEdit_vat->text());
    11. query.addBindValue(8, rowid);
    12. query.exec();
    To copy to clipboard, switch view to plain text mode 

    I assume that rowid is numeric.
    I recommend to use oracle style named placeholders, eg. ":my_bind_value".
    It makes the code more readable.
    Ad one last thing: RTFM.
    Insanity: doing the same thing over and over again and expecting different results.
    Albert Einstein

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

    Default Re: sql query problem

    Desktop_Qt_4_8_1_for_GCC__Qt_SDK__Debug/../../../QtSDK/Desktop/Qt/4.8.1/gcc/lib/QtSql.framework/Versions/4/Headers/qsqlquery.h:113: candidates are: void QSqlQuery::addBindValue(const QVariant&, QSql::ParamType)

  6. #6
    Join Date
    Aug 2008
    Location
    Ukraine, Krivoy Rog
    Posts
    1,963
    Thanked 370 Times in 336 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: sql query problem

    bindValue, use QSqlQuery::bindValue
    Qt Assistant -- rocks!
    please, use tags [CODE] & [/CODE].

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

    Default Re: sql query problem

    Thanks, what i miss is what to do with WHERE statement, because there is no example with this?

  8. #8
    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: sql query problem

    Search this site because there are plenty of examples littered throughout the forum. Or even do the experiment: try putting placeholders in the WHERE clause and find out... cannot take more than a few minutes to try.

Similar Threads

  1. Problem Executing Query from QT
    By pranavpunjabi in forum Qt Programming
    Replies: 5
    Last Post: 20th May 2011, 11:24
  2. problem while executing SQL query
    By sosanjay in forum Qt Programming
    Replies: 3
    Last Post: 9th October 2009, 12:52
  3. problem while executing Query
    By jjbabu in forum Qt Programming
    Replies: 1
    Last Post: 27th May 2009, 11:46
  4. Sql Query Problem
    By MrShahi in forum Qt Programming
    Replies: 6
    Last Post: 30th May 2008, 09:16

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.