Results 1 to 5 of 5

Thread: Using LIKE in parameterized SQL queries

  1. #1
    Join Date
    May 2009
    Posts
    15
    Thanks
    4
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Using LIKE in parameterized SQL queries

    I cannot get LIKE to work in the following scenario (Like and Field are initialized, just using a snippet...):
    Qt Code:
    1. QString Like; QString Field;
    2. query.prepare("SELECT Name, CustomerID FROM Customer WHERE ? LIKE ?");
    3. query.addBindValue(Field);
    4. query.addBindValue(QString("%%1\%").arg(Like));
    5. query.exec();
    To copy to clipboard, switch view to plain text mode 

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

    Default Re: Using LIKE in parameterized SQL queries

    Do you test the output of QString("%%1\%").arg(Like)? Why not use "\%%1\%" ?

  3. #3
    Join Date
    May 2009
    Posts
    15
    Thanks
    4
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Using LIKE in parameterized SQL queries

    The output of
    Qt Code:
    1. query.boundValue(1).toString()
    To copy to clipboard, switch view to plain text mode 
    is the same for both "%%1\%" AND "\%%1\%". A blank string for "Like" reads "%%", a filled string reads "%text%" both without double-quotes. That's why it's confusing to me. When replacing the query's '?' with the bound values given directly to the database or fed unparameterized to query.exec(), it works fine.

    If it matters, the database I'm using is MySQL, but I've had the same problem with SQLite, it's just that this instance matters much more than the other one.

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

    Default Re: Using LIKE in parameterized SQL queries

    Quote Originally Posted by MikeG View Post
    I cannot get LIKE to work in the following scenario (Like and Field are initialized, just using a snippet...):
    Qt Code:
    1. QString Like; QString Field;
    2. query.prepare("SELECT Name, CustomerID FROM Customer WHERE ? LIKE ?");
    3. query.addBindValue(Field);
    4. query.addBindValue(QString("%%1\%").arg(Like));
    5. query.exec();
    To copy to clipboard, switch view to plain text mode 
    Well I just noticed something else, is the QString Like ever like set to the string "like"?

  5. #5
    Join Date
    May 2009
    Posts
    15
    Thanks
    4
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Using LIKE in parameterized SQL queries

    Like is set by user input in a QLineEdit, so it could be "like", "Like", "LIKE" or anything really. Setting QString Like to any variation of 'like' didn't do anything (none of the data would match to "like" either right now). Field is set through a QComboBox.

Similar Threads

  1. Replies: 1
    Last Post: 4th June 2009, 17:44
  2. SQL Drivers Distribution
    By kandalf in forum Installation and Deployment
    Replies: 1
    Last Post: 12th January 2009, 09:17
  3. SQL date queries
    By ^NyAw^ in forum Qt Programming
    Replies: 1
    Last Post: 11th April 2008, 10:41
  4. Can I use the Sql Module without chanage .pro file?
    By fengtian.we in forum Qt Programming
    Replies: 9
    Last Post: 21st May 2007, 10:59
  5. SQL queries and slots
    By fnmblot in forum Newbie
    Replies: 8
    Last Post: 26th April 2007, 01:20

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.