FMDB和索引0超出空数组的边界

时间:2014-11-29 14:47:32

标签: ios objective-c xcode fmdb

我正在疯狂尝试调试这个。我试图基本上复制我的SQLite数据库中的一行并增加一个值。我在我的代码中的其他地方使用了类似的插入语句并且没有问题,但这次它不合作。程序在到达executeUpdate语句时出错。

我对iOS编程比较陌生,并且我已经尽我所能。

这是我的功能:

+ (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;
}

它不起作用。这是我崩溃时的结果:

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 个答案:

答案 0 :(得分:1)

您正在使用executeUpdateWithFormat,但未在SQL中使用printf样式语法。使用executeUpdateWithFormat格式字符串printf,或使用executeUpdate占位符?

就个人而言,我建议您只使用executeUpdate(并从列表末尾删除nil)。