iphone - NSInteger和int给出异常

标签 iphone objective-c ios nsinteger sqldatatypes

我正在尝试使用FMDB从数据库中获取数据,但是经过一段时间的徒劳尝试,我无法弄清楚为什么这会引发EXC_BAD_ACCESS异常。该参数是从NSInteger传入的,但是我既不能记录它,也不能在SELECT语句中使用它。我的代码是:

-(SQLiteResult*) getClient:(bool)forceRefresh id:(NSString*)id forYear:(NSInteger)year {
    SQLiteResult *res;
    if(!forceRefresh){
        NSMutableArray *hereditaments = [[NSMutableArray alloc] init];
        NSLog([NSString stringWithFormat:@"SELECT * FROM Clients WHERE ID = %@ AND Year = %@", id, [NSString stringWithFormat:@"%@", year]]); // exception
        NSLog([NSString stringWithFormat:@"SELECT * FROM Clients WHERE ID = %@ AND Year = %@", id, year]]); // also exception
        FMResultSet *rs = [db executeQuery:[NSString stringWithFormat: @"SELECT * FROM Clients WHERE ID = ? AND Year = %@", year], id]; // also exception
        FMResultSet *rs = [db executeQuery:[NSString stringWithFormat: @"SELECT * FROM Clients WHERE ID = ? AND Year = ?", id, year]]; // also exception

我濒临只是将其作为字符串传递。有没有安全的方法可以只在SQL语句中使用年份?

最佳答案

由于您的year变量为NSInteger类型,因此在创建NSString时应使用%d格式,而不要使用%@

关于iphone - NSInteger和int给出异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7010487/

相关文章:

ios - 省略 UIColor.setFill() 错误消息?

ios - iPhone 应用程序、数据层和数据库服务器

iPhone Quartz - 从其他类调用绘图方法

ios - 无法设置属性

objective-c - 在 iPhone 上使用 Objective C 将视频上传到 Vimeo

ios - iOS 11 中的 UITableViewCell 转换中断

iphone - 以编程方式触发 didEndOnExit

iphone - 恢复已完成交易已损坏?

ios - super View 将 subview 置于前面是行不通的。怎么做?

ios - 背景图像重叠刷新控件