FMDB and index 0 beyond bounds for empty array

162 Views Asked by At

I am going crazy trying to debug this. I am trying to basically duplicate a row in my SQLite db and increment a value. I have used similar insert statements elsewhere in my code and had no problems, but this time it is not cooperating. The program errors when it gets to the executeUpdate statement.

I am relatively new to iOS programming and have gone as far as I can on my own.

Here is my function:

+ (BOOL)updatePetWithNewVet:(NSNumber *)i {

    NSString *appSupportDir = [NSSearchPathForDirectoriesInDomains(NSApplicationSupportDirectory, NSUserDomainMask, YES) lastObject];
    NSString *dbPath = [appSupportDir stringByAppendingPathComponent:@"pets.sqlite"];
    FMDatabase *database = [FMDatabase databaseWithPath:dbPath];

    [database open];

    FMResultSet *results = [database executeQuery:@"Select * from vets order by vetID desc limit 1"];
    if ([results next]) {

        NSNumberFormatter * f = [[NSNumberFormatter alloc] init];
        [f setNumberStyle:NSNumberFormatterDecimalStyle];

        NSInteger vID = [results intForColumn:@"vetID"];
        NSInteger vID2 = vID+1;
        NSNumber *newVetID = [NSNumber numberWithInteger:vID2];
        NSString *vName = [results stringForColumn:@"vetName"];
        NSString *vPhone = [results stringForColumn:@"vetPhone"];
        NSString *vStreet = [results stringForColumn:@"vetStreet"];
        NSString *vCity = [results stringForColumn:@"vetCity"];
        NSString *vState = [results stringForColumn:@"vetState"];
        NSNumber *vZipcode = [f numberFromString:[results stringForColumn:@"vetZipcode"]];

        NSLog(@"%@", vName);
        NSLog(@"%@", vPhone);
        NSLog(@"%@", vStreet);
        NSLog(@"%@", vCity);
        NSLog(@"%@", vState);
        NSLog(@"%@", vZipcode);

        NSLog(@"VET ID: %ld", (long)vID);
        NSLog(@"NEW VET ID: %@", newVetID);

        [results close];

        [database executeUpdateWithFormat:@"INSERT INTO vets (vetID, vetName, vetPhone, vetStreet, vetCity, vetState, vetZipcode) Values (?, ?, ?, ?, ?, ?, ?)", newVetID, vName, vPhone, vStreet, vCity, vState, vZipcode, nil];
        NSLog(@"%@", [database lastErrorMessage]);
    }

    [database close];

    return 1;
}

And it is not working. This is what get when I crash:

2014-11-29 09:35:54.722 PetJournal[58823:8400345] *** Terminating app due to uncaught exception 'NSRangeException', reason: '*** -[__NSArrayM objectAtIndex:]: index 0 beyond bounds for empty array'
*** First throw call stack:
(
    0   CoreFoundation                      0x000000010eb37f35 __exceptionPreprocess + 165
    1   libobjc.A.dylib                     0x000000010e7d0bb7 objc_exception_throw + 45
    2   CoreFoundation                      0x000000010ea22f33 -[__NSArrayM objectAtIndex:] + 227
    3   PetJournal                          0x000000010cc1744e -[FMDatabase executeUpdate:error:withArgumentsInArray:orDictionary:orVAList:] + 2542
    4   PetJournal                          0x000000010cc17fa8 -[FMDatabase executeUpdate:withArgumentsInArray:] + 152
    5   PetJournal                          0x000000010cc1834d -[FMDatabase executeUpdateWithFormat:] + 637
    6   PetJournal                          0x000000010cc54fd7 +[UIView(Database) updatePetWithNewVet:] + 1159
    7   PetJournal                          0x000000010cc549d2 +[UIView(Database) createPetNamed:withBirthday:] + 1522
    8   PetJournal                          0x000000010cc3165e -[AddPetViewController savePet:] + 398
    9   UIKit                               0x000000010d07a8be -[UIApplication sendAction:to:from:forEvent:] + 75
    10  UIKit                               0x000000010d181410 -[UIControl _sendActionsForEvents:withEvent:] + 467
    11  UIKit                               0x000000010d1807df -[UIControl touchesEnded:withEvent:] + 522
    12  UIKit                               0x000000010d0c0308 -[UIWindow _sendTouchesForEvent:] + 735
    13  UIKit                               0x000000010d0c0c33 -[UIWindow sendEvent:] + 683
    14  UIKit                               0x000000010d08d9b1 -[UIApplication sendEvent:] + 246
    15  UIKit                               0x000000010d09aa7d _UIApplicationHandleEventFromQueueEvent + 17370
    16  UIKit                               0x000000010d076103 _UIApplicationHandleEventQueue + 1961
    17  CoreFoundation                      0x000000010ea6d551 __CFRUNLOOP_IS_CALLING_OUT_TO_A_SOURCE0_PERFORM_FUNCTION__ + 17
    18  CoreFoundation                      0x000000010ea6341d __CFRunLoopDoSources0 + 269
    19  CoreFoundation                      0x000000010ea62a54 __CFRunLoopRun + 868
    20  CoreFoundation                      0x000000010ea62486 CFRunLoopRunSpecific + 470
    21  GraphicsServices                    0x00000001114139f0 GSEventRunModal + 161
    22  UIKit                               0x000000010d079420 UIApplicationMain + 1282
    23  PetJournal                          0x000000010cc33f63 main + 115
    24  libdyld.dylib                       0x000000010f2d2145 start + 1
    25  ???                                 0x0000000000000001 0x0 + 1
)
1

There are 1 best solutions below

5
On BEST ANSWER

You are using executeUpdateWithFormat, but are not using printf-style syntax in your SQL. Either use executeUpdateWithFormat with printf-style format string, or use executeUpdate with the ? placeholders.

Personally, I'd suggest you just use executeUpdate (and drop the nil from the end of the list).