gpt4 book ai didi

ios - FMDB 和索引 0 超出空数组的范围

转载 作者:行者123 更新时间:2023-11-29 12:32:48 26 4
gpt4 key购买 nike

我正在疯狂地尝试调试它。我试图基本上复制我的 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 风格的语法。将 executeUpdateWithFormatprintf 样式的格式字符串一起使用,或者将 executeUpdate? 占位符一起使用。

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

关于ios - FMDB 和索引 0 超出空数组的范围,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27203508/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com