【问题标题】:NSString stringWithFormat unwanted resultNSString stringWithFormat 不需要的结果
【发布时间】:2012-02-07 23:53:51
【问题描述】:

我想做这样的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,我该如何防止呢?

【问题讨论】:

  • 仅供参考,这真的很容易受到注入攻击。我所要做的就是: date = @"'; drop table Table1; --"; poof 你的数据不见了。

标签: objective-c ios sqlite nsstring


【解决方案1】:

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

【讨论】:

    【解决方案2】:

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

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多