Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to form a LIKE statement in SQLITE with a parameter in iphone iOS?

Say, I have a searchfield called name. If the user types 'John' in the searchfield I should check the 'Name' field for the value "John" and select all the corresponding field values. 'John' will be of NSString which is got from searchBar.text, and it has to be added as parameter to the query. I structured a sql statement like this which never worked.

NSString *query = [NSString stringWithFormat:@"select * from Employee where EmployeeName LIKE '%%@%'", searchText.text];
const char *sqlStatement = [query UTF8String];

I tried this also

NSString *query = [NSString stringWithFormat:@"select * from Employee where EmployeeName LIKE %'%@'%", searchText.text];
const char *sqlStatement = [query UTF8String];

Nothing worked.

like image 905
nOOb iOS Avatar asked Nov 28 '22 11:11

nOOb iOS


1 Answers

The trick is to get the percents in there. It's awkward, but you can do it like this:

@"select * from Employee where EmployeeName LIKE '%%%@%%'"

%% is a percent, %@ is the substitution string.

like image 135
danh Avatar answered Dec 05 '22 15:12

danh