Results 1 to 5 of 5

Thread: QSqlQuery and single quotes using bindValue

  1. #1
    Join Date
    Jan 2010
    Posts
    190
    Thanks
    18
    Thanked 1 Time in 1 Post
    Qt products
    Qt4 Qt5 Qt/Embedded
    Platforms
    MacOS X Unix/X11 Windows Android

    Default QSqlQuery and single quotes using bindValue

    Hi! I created some tables using sqlite with Qt. My problem is that I noticed that using bindValue this way:
    Qt Code:
    1. query.prepare("INSERT INTO monitored_files "
    2. "VALUES (NULL, :fileName, :original_relative_dir_path, :backupped_relative_dir_path, :directory_id)");
    3. query.bindValue(":fileName", entries.at(i));
    4. query.bindValue(":original_relative_dir_path", processRelativeDirPath);
    5. query.bindValue(":backupped_relative_dir_path", processRelativeDirPath);
    6. query.bindValue(":directory_id", directoryId);
    7. query.exec();
    To copy to clipboard, switch view to plain text mode 
    where entries.at(i) is P9040479.JPG and processRelativeDirPath is "." (without "s), I get this when listing the records in sqlite:
    Qt Code:
    1. 1|'P9040479.JPG'|'.'|'.'|1
    To copy to clipboard, switch view to plain text mode 
    this seems to be wrong as, if I write the query directly in sqlite:
    Qt Code:
    1. insert into monitored_files values (null, 'P9040479.JPG', '.', '.', 1);
    To copy to clipboard, switch view to plain text mode 
    I get:
    Qt Code:
    1. 2|P9040479.JPG|.|.|1
    To copy to clipboard, switch view to plain text mode 
    The schema of my table is:
    Qt Code:
    1. CREATE TABLE monitored_files (file_id INTEGER PRIMARY KEY,file_name VARCHAR(32767),original_relative_dir_path VARCHAR(32767),backupped_relative_dir_path VARCHAR(32767),directory_id INTEGER);
    To copy to clipboard, switch view to plain text mode 
    So it seems those Qt instructions are placing single quotes that shouldn't be there... My variables don't have those single quotes. Any idea where they come from?
    Thanks!

  2. #2
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,364
    Thanks
    3
    Thanked 5,013 Times in 4,791 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: QSqlQuery and single quotes using bindValue

    If you select the data back, do you have the quotes in the result?
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  3. #3
    Join Date
    Jan 2010
    Posts
    190
    Thanks
    18
    Thanked 1 Time in 1 Post
    Qt products
    Qt4 Qt5 Qt/Embedded
    Platforms
    MacOS X Unix/X11 Windows Android

    Default Re: QSqlQuery and single quotes using bindValue

    If I understand correctly your question then yes, I get the single quotes.

    I queried the table with a:
    Qt Code:
    1. select * from monitored_files
    To copy to clipboard, switch view to plain text mode 
    and I printed all the file_name's. The result is the field with the quotes.
    My problem started when I tried to query according to the value of the field. Using a simple:
    Qt Code:
    1. select * from monitored_files where file_name='something'
    To copy to clipboard, switch view to plain text mode 
    returns not records even if that string is in the table. The same is for "... LIKE '%something'" and "... LIKE 'something%'", but not "... LIKE '%something%'". I suppose this is because the single quotes are stored in the database. Any idea why bindValue is placing the single quotes twice?
    Thanks!

  4. #4
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,364
    Thanks
    3
    Thanked 5,013 Times in 4,791 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: QSqlQuery and single quotes using bindValue

    This works for me:
    Qt Code:
    1. #include <QtSql>
    2. #include <QtGui>
    3.  
    4. int main(int argc, char **argv){
    5. QApplication app(argc, argv);
    6. QSqlDatabase db = QSqlDatabase::addDatabase("QSQLITE");
    7. db.setDatabaseName(":memory");
    8. db.open();
    9. QSqlQuery qry("CREATE TABLE test (id integer, path varchar)");
    10. qry.prepare("INSERT INTO test VALUES(1, :path)");
    11. qry.bindValue(":path", "somedata");
    12. qry.exec();
    13. qry.exec("SELECT * FROM test");
    14. while(qry.next()){
    15. qDebug() << qry.value(0) << qry.value(1);
    16. }
    17.  
    18. }
    To copy to clipboard, switch view to plain text mode 

    Result: QVariant(qlonglong, 1) QVariant(QString, "somedata")
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


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

    Luc4 (27th September 2010)

  6. #5
    Join Date
    Jan 2010
    Posts
    190
    Thanks
    18
    Thanked 1 Time in 1 Post
    Qt products
    Qt4 Qt5 Qt/Embedded
    Platforms
    MacOS X Unix/X11 Windows Android

    Default Re: QSqlQuery and single quotes using bindValue

    It works for me too, indeed. I just tried to run my code in release mode and it's working... I suspect I have some problems not with the code, but with the way the new Qt Creator manages the builds. Thank you very much.

Similar Threads

  1. Compilation problem with QSqlQuery::bindValue()
    By brevleq in forum Qt Programming
    Replies: 2
    Last Post: 18th November 2013, 23:30
  2. QSqlQuery::bindValue
    By viglu in forum Newbie
    Replies: 3
    Last Post: 29th March 2010, 21:13
  3. QSqlQuery::bindValue() Question
    By kandalf in forum Qt Programming
    Replies: 7
    Last Post: 30th January 2010, 13:14
  4. QSqlQuery, bindValue and Sqlite
    By cydside in forum Qt Programming
    Replies: 4
    Last Post: 5th April 2009, 17:53
  5. QSqlQuery::bindValue problem
    By segfault in forum Qt Programming
    Replies: 6
    Last Post: 11th March 2009, 08: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
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.