我有一个看起来像这样的SQL查询 -
NSString *createSQL = @"SELECT ingredients, recipe FROM drinktable where title like '%_drinkName%'";
_drinkName 也是一个变量。在objective-c中写这个的正确语法是什么?
答案 0 :(得分:2)
假设_drinkName是NSString,请尝试:
NSString *createSQL = [NSString stringWithFormat:@"SELECT ingredients, recipe FROM drinktable WHERE title LIKE '%%%@%%'", _drinkName];
(请注意,每个%的%需要加倍。%@用于字符串格式参数。)
答案 1 :(得分:0)
因为NSString是一个对象,所以您可以使用stringWithFormat
将%@
说明符替换为另一个字符串
NSString *createSQL = [NSString stringWithFormat: @"SELECT ingredients, recipe FROM drinktable where title like '%@'", _drinkName];
这会将%@
替换为_drinkName