我快要调试这个了。我试图基本上在我的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
)
最佳答案
您正在使用executeUpdateWithFormat
,但未在SQL中使用printf
-style语法。可以将executeUpdateWithFormat
与printf
-style格式的字符串一起使用,或者将executeUpdate
与?
占位符一起使用。
就个人而言,我建议您只使用executeUpdate
(并将nil
从列表末尾删除)。
关于ios - FMDB和索引0超出了空数组的范围,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/27203508/