+ Reply to Thread
Results 1 to 4 of 4

Thread: QSqlQuery::bindValue does not work

  1. #1
    Join Date
    Sep 2012
    Posts
    3
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default QSqlQuery::bindValue does not work

    Hi.

    Here is the code:

    Qt Code:
    1. QSqlQuery insQuery(*webDb);
    2. insQuery.prepare("INSERT INTO crawl_virtuemart_categories (virtuemart_category_id, virtuemart_vendor_id, category_layout, category_product_layout, products_per_row, limit_list_start, limit_list_step, limit_list_max, limit_list_initial, ordering, finished) VALUES ( :id , 1, \"default\", \"default\", 3, 0, 10, 0, 10, 1, 1)");
    3. insQuery.bindValue(":id",100);
    To copy to clipboard, switch view to plain text mode 

    Unfortunately, after execution I have syntax error, because of remaining :id instead of 100. But why? Shouldn't I mix "hardcoded" values with "dynamic" ones?

    Thanks in advance.
    Last edited by l0ud; 22nd September 2012 at 23:54.

  2. #2
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,468
    Thanks
    13
    Thanked 1,564 Times in 1,494 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: QSqlQuery::bindValue does not work

    Sql string values are enclosed by single quotes not double quotes: this is the syntax error.

  3. #3
    Join Date
    Sep 2012
    Posts
    3
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QSqlQuery::bindValue does not work

    It's not! I forgot to add this is mysql.

    But anyway, the following code gives the same error:

    Qt Code:
    1. QSqlQuery insQuery(*webDb);
    2. insQuery.prepare("INSERT INTO crawl_virtuemart_categories (virtuemart_category_id, virtuemart_vendor_id, category_layout, category_product_layout, products_per_row, limit_list_start, limit_list_step, limit_list_max, limit_list_initial, ordering, finished) VALUES ( :id , 1, 'default', 'default', 3, 0, 10, 0, 10, 1, 1)");
    3. insQuery.bindValue(":id",100);
    To copy to clipboard, switch view to plain text mode 


    you have error in your syntax near ':id , 1, 'default', ...

  4. #4
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,468
    Thanks
    13
    Thanked 1,564 Times in 1,494 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: QSqlQuery::bindValue does not work

    Quote Originally Posted by l0ud View Post
    It's not! I forgot to add this is mysql.
    Well, since you didn't actual bother to specify either the actual error message or the database engine I assumed ANSI SQL, which does not allow double-quotes as literal string delimiters. MySQL will not accept double quotes if ANSI mode is turned on, so if you do not have complete control of the MySQL database settings and expect portability then you should use single quotes. Quite apart from that, single quotes are both easier to type and read in C++ code.
    But anyway, the following code gives the same error:
    ...
    You don't say what your table definition actually is. This, for example, works just fine:
    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. QSqlDatabase db = QSqlDatabase::addDatabase("QMYSQL");
    9. db.setHostName("newton");
    10. db.setDatabaseName("test");
    11. db.setUserName("test");
    12. db.setPassword("password");
    13. bool ok = db.open();
    14. qDebug() << "Opened:" << ok;
    15.  
    16. db.exec(
    17. "CREATE TABLE IF NOT EXISTS crawl_virtuemart_categories ("
    18. " virtuemart_category_id int(11) NOT NULL,"
    19. " virtuemart_vendor_id int(11) NOT NULL,"
    20. " category_layout varchar(10) NOT NULL,"
    21. " category_product_layout varchar(10) NOT NULL,"
    22. " products_per_row int(11) NOT NULL,"
    23. " limit_list_start int(11) NOT NULL,"
    24. " limit_list_step int(11) NOT NULL,"
    25. " limit_list_max int(11) NOT NULL,"
    26. " limit_list_initial int(11) NOT NULL,"
    27. " ordering int(11) NOT NULL,"
    28. " finished int(11) NOT NULL,"
    29. " PRIMARY KEY (virtuemart_category_id)"
    30. ") "
    31. );
    32.  
    33. // Your code
    34. QSqlQuery insQuery;
    35. insQuery.prepare("INSERT INTO crawl_virtuemart_categories (virtuemart_category_id, virtuemart_vendor_id, category_layout, category_product_layout, products_per_row, limit_list_start, limit_list_step, limit_list_max, limit_list_initial, ordering, finished) VALUES ( :id , 1, 'default', 'default', 3, 0, 10, 0, 10, 1, 1)");
    36. insQuery.bindValue(":id",100);
    37. // end of your code
    38.  
    39. ok = insQuery.exec();
    40. qDebug() << "Insert:" << ok;
    41. if (!ok)
    42. qDebug() << insQuery.lastError();
    43.  
    44. return 0;
    45. }
    To copy to clipboard, switch view to plain text mode 
    (provided a row with id == 100 does not already exist)

+ Reply to Thread

Similar Threads

  1. QSqlQuery bindValue and SELECT WHERE IN
    By helloworld in forum Qt Programming
    Replies: 5
    Last Post: 6th November 2010, 15:45
  2. QSqlQuery::bindValue
    By viglu in forum Newbie
    Replies: 3
    Last Post: 29th March 2010, 20:13
  3. QSqlQuery::bindValue() Question
    By kandalf in forum Qt Programming
    Replies: 7
    Last Post: 30th January 2010, 12:14
  4. QSqlQuery, bindValue and Sqlite
    By cydside in forum Qt Programming
    Replies: 4
    Last Post: 5th April 2009, 16:53
  5. QSqlQuery::bindValue problem
    By segfault in forum Qt Programming
    Replies: 6
    Last Post: 11th March 2009, 07:27

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