我想从数据库中加载 tableView 值,用于测试目的 database.sqlite。它填充了以下内容:
1、一月
2、二月
... 向下 ...
12 月 12 日
如您所见,我正在使用 FMDB。以下行编译但崩溃:
NSString *myString = [[rows objectAtIndex:myNumber] objectForKey:@"MonthName"];
我在处理 Cocoa 中的 Ints 和 String 的不一致方式时遇到了问题,这是我的问题的一部分。此外,虽然那里有 NSLog 调试行,但这种方法看起来很复杂。你能给我什么建议?
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *docsPath = [paths objectAtIndex:0];
NSString *path = [docsPath stringByAppendingPathComponent:@"database.sqlite"];
FMDatabase *database = [FMDatabase databaseWithPath:path];
[database open];
NSLog(@"Open Database");
FMResultSet *results = [database executeQuery:@"SELECT * FROM monthly"];
while([results next]) {
NSString *name = [results stringForColumn:@"name"];
NSInteger age = [results intForColumn:@"age"];
// Stuff Data into Array
NSMutableArray *rows = [[NSMutableArray alloc] init];
NSMutableDictionary *firstRow = [NSMutableDictionary dictionaryWithObjectsAndKeys: [NSNumber numberWithInt:age], @"MonthID", [NSString stringWithFormat:@"%@", name], @"MonthName", nil];
[rows addObject:firstRow];
NSLog(@"Month: %@ - %d",name, age);
int myNumber = [[firstRow objectForKey:@"MonthID"] intValue];
// *** THE LINE BELOW IS GIVING ME PROBLEMS ***
NSString *myString = [[rows objectAtIndex:myNumber] objectForKey:@"MonthName"];
// NSLog(@"Month No: %@ - %d",myString, myNumber);
NSLog(@"***** Month No: %d", myNumber);
// [self.monthMonths myString];
}
[database close];
NSLog(@"Close Database");