FMDB query isn't acting right with LIKE
Asked Answered
T

1

14

I am using FMDB, which is a wrapper for SQLite. http://github.com/ccgus/fmdb

Here is my query line:

FMResultSet *athlete = [db executeQuery:@"SELECT * FROM athletes WHERE athlete_name LIKE ?", search_text];

Using this I can get a result back if I type in the exact name of the athlete. But, I'm trying to use LIKE so I can search on the name. But, when I add %?% instead of just ? ... nothing returns. And there are no errors.

Has anyone ran into this before and know what I'm doing wrong?

Thanks everyone!

Theophilus answered 25/1, 2012 at 19:9 Comment(1)
i'd suggest providing a link to FMDB for those unfamiliar with itSanguine
A
35

The wildcard characters (%) have to be part of the substituted variable, not the query string:

FMResultSet *rs = [db executeQuery:@"SELECT * FROM athletes WHERE athlete_name LIKE ?",
                  [NSString stringWithFormat:@"%%%@%%", search_text]];
Allbee answered 25/1, 2012 at 19:24 Comment(4)
Ah ok ... thank you very much. I'm 1/2 asleep today, so I assumed I was missing something simple. =)Theophilus
I'm having difficulty getting this to work. Is there an alternative syntax? I'm running out of ideas.Resemble
Ah nice!! Thanks @omz. I'm extremely new to mysql so would you mind explaining what the %%%@%% means? I get the %@... but what is the significance of the other %'s? And the ?. I'm assuming the ? is the sql equivalent to %@ but I still get this warning from it: Data argument not used by format stringDyspnea
The % is a "wildcard" for the SQL LIKE operator. Think of it as a placeholder that allows you to match parts of a string. Because % has a special meaning in format strings, each of it is doubled to indicate "I want an actual percent character, not a format string placeholder like %@". This is only necessary because I use stringWithFormat: here. You're right that the ? is basically the SQL equivalent of %@.Allbee

© 2022 - 2024 — McMap. All rights reserved.