Ashish Agarwal
Ashish Agarwal

Reputation: 14925

NSString containing sql query in objective-c

I have a sql query which looks like this -

NSString *createSQL = @"SELECT ingredients, recipe FROM drinktable where title like '%_drinkName%'";

Also _drinkName is a variable. What is the correct syntax of writing this in objective-c ?

Upvotes: 0

Views: 774

Answers (2)

Robotnik
Robotnik

Reputation: 3800

Because NSString is an object, you can use stringWithFormat to replace the %@ specifier with another string

NSString *createSQL = [NSString stringWithFormat: @"SELECT ingredients, recipe FROM drinktable where title like '%@'", _drinkName];

This will replace %@ with the value of _drinkName

Upvotes: 0

Turix
Turix

Reputation: 4490

Assuming _drinkName is an NSString, try:

NSString *createSQL = [NSString stringWithFormat:@"SELECT ingredients, recipe FROM drinktable WHERE title LIKE '%%%@%%'", _drinkName];

(Note that each of your %'s needs to be doubled. And the %@ is for the string format parameter.)

Upvotes: 2

Related Questions