Results 1 to 6 of 6

Thread: SQLite Problem

  1. #1
    Join Date
    Nov 2007
    Posts
    5
    Thanks
    1
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default SQLite Problem

    Hi!!

    Basically I want to do a simplified keyword search using the following SQL query;

    SELECT body,created_on FROM entries WHERE body LIKE '%term%' ORDER BY created_on;

    In the SQLite command line I receive the result set I expect. When I transfer it to Qt it simply does not work. I have tried the following;

    Qt Code:
    1. QSqlQuery query;
    2. query.prepare( "SELECT body,created_on FROM entries WHERE body LIKE '%?%' ORDER BY created_on" );
    3. query.addBindValue( term );
    4. query.exec();
    To copy to clipboard, switch view to plain text mode 

    As well as named bindings;

    Qt Code:
    1. QSqlQuery query;
    2. query.prepare( "SELECT body,created_on FROM entries WHERE body LIKE '%:term%' ORDER BY created_on" );
    3. query.bindValue( ":term", term );
    4. query.exec();
    To copy to clipboard, switch view to plain text mode 

    The QSqlError I get in both cases is reported as 2. The error text is "parameter count mismatch" in both cases.

    Note a similar query applied without a conditional works fine in QT. The query that works is as follows;

    Qt Code:
    1. query.prepare( "SELECT body,created_on FROM entries ORDER BY created_on" );
    To copy to clipboard, switch view to plain text mode 

    Hard coding a value also works;

    Qt Code:
    1. query.prepare( "SELECT body,created_on FROM entries WHERE body LIKE '%term%' ORDER BY created_on" );
    To copy to clipboard, switch view to plain text mode 

    Obviously there is something I am missing. Are % not allowed when used with a bound value? I've skimmed through the documentation and haven't seen anything that would imply such limitations.

    Any help would be greatly appreciated.

    Nate

    System Info:
    ~~~~~~~~
    Mac OS X Tiger
    QT 4.3.2
    g++ v4.0.1

  2. #2
    Join Date
    Oct 2007
    Location
    Munich, Bavaria
    Posts
    144
    Thanks
    1
    Thanked 19 Times in 19 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: SQLite Problem

    Searching the Qt sources I did find the error message in the SQLite driver.
    Looks like your query is not compliant with the SQLite definitions.

    Let's have a look at the SQLite documentation ...

    Good luck,

    Tom
    Last edited by DeepDiver; 7th November 2007 at 10:57. Reason: updated contents

  3. #3
    Join Date
    Jan 2006
    Posts
    369
    Thanks
    14
    Thanked 18 Times in 17 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: SQLite Problem

    "like" in sqlite3 works in a funky way, take a look into it's definition in that DB (or do the filtering in c++ code).

  4. #4
    Join Date
    Nov 2007
    Posts
    5
    Thanks
    1
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: SQLite Problem

    Quote Originally Posted by elcuco View Post
    "like" in sqlite3 works in a funky way, take a look into it's definition in that DB (or do the filtering in c++ code).
    I'm pretty sure it's not at the DB level because the last example I gave works in QT which includes a like clause, it just doesn't use bound values. From what I've read in the documentation I'm left with the impression that sometimes bound values are deferred to the database to handle and other times implemented directly in QT dependent on what the database can handle. I'll try wading through the source, I think that's going to be my best bet at this point.

    Thanks for the replies,

    Nate

  5. #5
    Join Date
    Oct 2007
    Location
    Munich, Bavaria
    Posts
    144
    Thanks
    1
    Thanked 19 Times in 19 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: SQLite Problem

    In case of SQLite bound values are NOT handled by Qt but by the driver.
    That's why the error message comes from the driver sources.

    You need to have a look at the SQLite docs on host variable definition.
    Maybe you ask in a SQLite forum ....


    Good luck,

    Tom

  6. #6
    Join Date
    Nov 2007
    Posts
    1
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: SQLite Problem

    I had the same problem. To solve the problem, use the string with wildcards as your bindValue. However, do not use the apostrophes. I also used the ":" syntax. Here is an example that should work.

    QString qsBind = "%" + qsYourString + "%";

    query.prepare("SELECT * FROM People WHERE Name LIKE :contains;");
    query.bindValue(":contains", qsBind);
    query.exec();

    Hope this helps.

  7. The following user says thank you to SSS for this useful post:

    natbobc (16th November 2007)

Similar Threads

  1. [QMYSQL] connection problem
    By chaos_theory in forum Installation and Deployment
    Replies: 5
    Last Post: 2nd July 2007, 09:52
  2. Problem with SqLite and Qt
    By ad5xj in forum Newbie
    Replies: 26
    Last Post: 5th June 2007, 01:53
  3. Grid Layout Problem
    By Seema Rao in forum Qt Programming
    Replies: 2
    Last Post: 4th May 2006, 12:45
  4. Problem with bitBlt
    By yellowmat in forum Newbie
    Replies: 1
    Last Post: 5th April 2006, 14:08
  5. Replies: 16
    Last Post: 7th March 2006, 15:57

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.