1

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?

EmptyStack
  • 51,274
  • 23
  • 147
  • 178
Echilon
  • 10,064
  • 33
  • 131
  • 217

2 Answers2

3

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.

EmptyStack
  • 51,274
  • 23
  • 147
  • 178
0

As your year variable is of NSInteger type you should use %d format and not %@ in your NSString creation

gsempe
  • 5,371
  • 2
  • 25
  • 29