NSInteger 和 int 给出例外
我正在尝试使用 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 语句中使用年份?
I'm trying to get data from a database using FMDB but having tried with futility for a while, I can't figure out why this is throwing an EXC_BAD_ACCESS exception. The parameter is passed in from an NSInteger, but I can neither log it nor use it in a SELECT statement. The code I have is:
-(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
I'm on the verge of just passing it in as a string instead. Is there a safe way I can just use the year in the SQL statement?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
NSInteger
不是对象类型。它只是一个int
类型的typedef
。因此,您不能使用%@
作为格式说明符。您应该使用%d
。NSInteger
is not an object type. Its just atypedef
of typeint
. So, you can not use%@
as the format specifier. You should use%d
.由于您的年份变量是 NSInteger 类型,因此您应该在 NSString 创建中使用 %d 格式而不是 %@
As your year variable is of NSInteger type you should use %d format and not %@ in your NSString creation