NSInteger and int giving exceptions
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 executeQ开发者_运维百科uery:[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?
NSInteger
is not an object type. Its just a typedef
of type int
. So, you can not use %@
as the format specifier. You should use %d
.
As your year variable is of NSInteger type you should use %d format and not %@ in your NSString creation
精彩评论