Results 1 to 5 of 5

Thread: QSqlQuery, bindValue and Sqlite

  1. #1
    Join Date
    Jun 2008
    Location
    Rome, Italy
    Posts
    95
    Thanks
    19
    Thanked 2 Times in 2 Posts
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows

    Default QSqlQuery, bindValue and Sqlite

    Hi to all,
    I'm having some problems binding values in a "SELECT" statement on a table, maybe the code will explain better:

    Qt Code:
    1. void frmLogin::submit()
    2. {
    3. QString strQuery;
    4. QString strName;
    5. QString strPass;
    6.  
    7. strQuery = "SELECT * FROM tbUtente WHERE utenteName=':name';"; //doesn't work
    8. //strQuery = "SELECT * FROM tbUtente;"; //it works
    9. strName = ui.lneUser->text();
    10. strPass = ui.lnePwd->text();
    11. /*
    12.   qDebug() << "Nome: " << strName;
    13.   qDebug() << "Pass: " << strPass;
    14.   */
    15.  
    16. QSqlQuery queryUser;
    17. queryUser.prepare(strQuery);
    18. queryUser.bindValue(":name", strName); //doesn't work
    19. // without bind a value it works
    20. queryUser.exec();
    21.  
    22. while (queryUser.next())
    23. {
    24. qDebug() << "Nome: " << queryUser.value(1).toString();
    25. qDebug() << "Pass: " << queryUser.value(2).toString();
    26. }
    27.  
    28. QSqlRecord rec = queryUser.record();
    29.  
    30. if (rec.count() >= 1)
    31. {
    32. this->accept();
    33. qDebug() << "Ok submit!";
    34. }
    35. else
    36. {
    37.  
    38. ui.lneUser->clear();
    39. ui.lnePwd->clear();
    40. ui.lneUser->setFocus();
    41. }
    42.  
    43. userName = ui.lneUser->text();
    44. }
    To copy to clipboard, switch view to plain text mode 

    so, if I use the "//doesn't work" instrutcions (line 7 and line 18) it doesn't return a value, instead, if I comment line 7 and 18 and run line 8 it returns all the values!!!
    Why it happens?!

    thanks in advance
    PS: I'm using Qt 4.5.0, Sqlite3 database and WinXP.

  2. #2
    Join Date
    Nov 2008
    Location
    Częstochowa/Poland
    Posts
    50
    Thanks
    2
    Thanked 11 Times in 11 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Wink Re: QSqlQuery, bindValue and Sqlite

    Try it like this:
    Qt Code:
    1. strQuery = "SELECT * FROM tbUtente WHERE utenteName = :name"
    To copy to clipboard, switch view to plain text mode 

    no ' Qt takes care of that for You.

  3. The following user says thank you to Grimlock for this useful post:

    miwarre (8th November 2009)

  4. #3
    Join Date
    Feb 2009
    Location
    Noida, India
    Posts
    517
    Thanks
    21
    Thanked 66 Times in 62 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QSqlQuery, bindValue and Sqlite

    try removing single quotes around :name in strQuery

  5. The following user says thank you to talk2amulya for this useful post:

    cydside (5th April 2009)

  6. #4
    Join Date
    Jun 2008
    Location
    Rome, Italy
    Posts
    95
    Thanks
    19
    Thanked 2 Times in 2 Posts
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows

    Talking Re: QSqlQuery, bindValue and Sqlite

    Thank you!!!!!

  7. #5
    Join Date
    Nov 2008
    Location
    Częstochowa/Poland
    Posts
    50
    Thanks
    2
    Thanked 11 Times in 11 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QSqlQuery, bindValue and Sqlite

    You're welcome

  8. The following user says thank you to Grimlock for this useful post:

    cydside (5th April 2009)

Similar Threads

  1. Sqlite and UTF8 data
    By kroenecker in forum Qt Programming
    Replies: 2
    Last Post: 19th April 2009, 14:49

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.