Using% -sign (percentage symbol) in SQL query with objective-c

So, exactly what the title says. I would like to use a query like this:

NSString *querySQL = [NSString stringWithFormat:@"SELECT COUNT (*) FROM Devices WHERE Location LIKE '%@%'", name];

Basically it asks SELECT COUNT (*) FROM Devices WHERE Location LIKE 'N%'. Then I would like to find all the computers in Building N, including all subzones (e.g. N33).

The question arises: how to insert% -sign into the request? Currently it only shows 'N'...

+3
source share
1 answer

Use two% signs to go to%:

NSString *querySQL = [NSString stringWithFormat:
     @"SELECT COUNT (*) FROM Devices WHERE Location LIKE '%@%%'", name];

(of course, you need to misinform the input or use a parameterized query first to prevent SQL injection attacks)

+14
source

All Articles