gpt4 book ai didi

iphone - NSInteger和int给出异常

转载 作者:行者123 更新时间:2023-12-01 19:26:36 26 4
gpt4 key购买 nike

我正在尝试使用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/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com