Results 1 to 3 of 3

Thread: placeholder problem

  1. #1
    Join Date
    Jun 2015
    Posts
    28
    Qt products
    Qt5
    Platforms
    Unix/X11

    Default placeholder problem

    Hello,
    I'm with problem in my UPDATE clause using placeholder (sqlite3). In some fields the placeholder return a incorrect data, even i verifying that the data that i send to UPDATE is correct.

    Eg:the output from "query2.value(0).toInt()" is 2 (a integer value, like i expected), but the placeholder ":id_city" stores the following value: 1_city

    Qt Code:
    1. QSqlDatabase con = QSqlDatabase::addDatabase("QSQLITE");
    2. con.setDatabaseName(_DB);
    3. con.open();
    4.  
    5. QSqlQuery query;
    6. // the problem occur ins some cases with different data types, like integer directly (0), query2 return and text from qlineedit, as with diferent object types (qlineedit, qcombobox, qcheckbox)
    7. query.prepare("UPDATE table SET id_country=:id_country, id_city=:id_city, name=:name ...... WHERE id=:id"
    8. query.bindValue(":id_country",1);
    9. query.bindValue(":id_city",query2.value(0).toInt());
    10. query.bindValue(":name",ui->QLineedit->text());
    11. .
    12. .
    13. .
    14. query.bindValue(":id",_ID);
    15. query.exec();
    To copy to clipboard, switch view to plain text mode 

    someone known where is the problem? could help me please?

    Thanks!
    Juliano

  2. #2
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: placeholder problem

    Since "1_city" looks like the ":id" part got replaced, have you tried using placeholders without _ ?

    Cheers,
    _

  3. #3
    Join Date
    Jun 2015
    Posts
    28
    Qt products
    Qt5
    Platforms
    Unix/X11

    Default Re: placeholder problem

    Hello,

    Thanks anda_skoa, after several testing I learned:

    1. dont use _ in placeholders;
    2. dont use number in begin of a table field (my real table has this structure. eg: 1_city (to remember me that this field belongs to group 1));
    3. if the error "parameter count mismatch" is showed when building de code, check the SQL statement, probably it have some problem.

    A good way I've found to check my SQL statement with placeholders:
    Qt Code:
    1. QString getLastExecutedQuery(const QSqlQuery& query)
    2. {
    3. QString str = query.lastQuery();
    4. QMapIterator<QString, QVariant> it(query.boundValues());
    5. while (it.hasNext())
    6. {
    7. it.next();
    8. str.replace(it.key(),it.value().toString());
    9. }
    10. return str;
    11.  
    12. // call it after your query.exec(), like "cout << getLastExecutedQuery(query).toStdString();"
    13. }
    To copy to clipboard, switch view to plain text mode 

    solved

    hugs!
    Juliano
    Last edited by juliano.gomes; 30th November 2015 at 18:57.

Similar Threads

  1. QSqlQuery::prepare() - placeholder as table name
    By dawwin in forum Qt Programming
    Replies: 2
    Last Post: 7th March 2011, 21:40

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.