NSString stringWithFormat不需要的结果

时间:2012-01-11 16:02:22

标签: objective-c ios sqlite nsstring

我想像这样制作sql select语句:

NSLog(@"sql = %@",[NSString stringWithFormat:@"select * from Table1 Where ID = %i AND  '%@' = strftime('%Y',startDate)",ID,date]);

但它打印给我:

sql = select * from Table1 Where ID = 1 AND  '2012' = strftime('Y',startDate)

%Y变为Y,我怎么能阻止这个?

2 个答案:

答案 0 :(得分:4)

添加另一个%以转义%字符:

NSLog(@"sql = %@",[NSString stringWithFormat:@"select * from Table1 Where ID = %i AND  '%@' = strftime('%%Y',startDate)",ID,date]);

答案 1 :(得分:1)

添加类似NSLog(@"sql = %@",[NSString stringWithFormat:@"select * from Table1 Where ID = %i AND '%@' = strftime('%%Y',startDate)",ID,date]);

的%