在字符串中转义sqlite格式说明符的正确方法是什么

时间:2012-01-30 15:34:27

标签: objective-c sqlite stringwithformat

在字符串中转义sqlite格式说明符的正确方法是什么?

    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
    [dateFormatter setDateFormat:@"yyyy-MM-dd"];

    NSDate *today = [NSDate date];
    NSString *todaysDate = [dateFormatter stringFromDate:today];

    NSString *SQL = [NSString stringWithFormat: @"SELECT id,strftime(\"%Y-%m-%d\") as `date`,word,term FROM words WHERE `date` <= '%@' ORDER BY `date` DESC LIMIT 1",todaysDate];

由于

1 个答案:

答案 0 :(得分:0)

想出来。要转义“%”,请使用“%%”。

NSString *SQL = [NSString stringWithFormat: @"SELECT id,strftime(\"%%Y-%%m-%%d\") as `date`,word,term FROM words WHERE `date` <= '%@' ORDER BY `date` DESC LIMIT 1",todaysDate];