我正在使用SQLite3从数据库中获取数据。所以我试图创建一个Multidimensional数组来保存所有记录的数组,这些记录是数组中的嵌套数组。所以当循环到达这段代码时:
[[studentsInfo objectAtIndex:recordRow] addObject: [NSMutableArray arrayWithObjects:studentLastName, studentFirstName, studentID, studentEmail, programName, majorityCampus, discipline, cmName, cmID, cmEmail, cmPhone, cmCampus, studentNotes, nil]];
它崩溃了。这是我从Xcode获得的。
这是完整的代码我做错了什么?
- (void) initializeDatabase {
// get the documents directory
NSArray *dirPaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *docsDir = dirPaths[0];
NSLog(@"Directory Path: %@", docsDir);
FindYourMentorDB_Path = [[NSString alloc]initWithString:[docsDir stringByAppendingPathComponent:@"FindYourCM_Info.sqlite"]];
NSFileManager *fileManager = [NSFileManager defaultManager];
if ([fileManager fileExistsAtPath:FindYourMentorDB_Path] == NO) {
// attempt to open the db
if (sqlite3_open([FindYourMentorDB_Path UTF8String], &_FindYourMentorDB) == SQLITE_OK){
sqlite3_close(_FindYourMentorDB); // Close the DB
NSLog(@"Succeeded in opening FindYourMentorDB");
} else {
NSLog(@"Failed to Open the FindYourMentorDB");
} // END OF IF
} // END OF IF
NSMutableArray *studentsInfo = [NSMutableArray new];
sqlite3_stmt *statement;
if (sqlite3_open([FindYourMentorDB_Path UTF8String], &_FindYourMentorDB) == SQLITE_OK) {
NSString *selectSQL = [NSString stringWithFormat:@"SELECT StudentLastName, StudentFirstName, StudentID, StudentEmail, ProgramName, MajorityCampus, Discipline, CMName, CMID, CMEmail, CMPhone, CMCampus, StudentNotes FROM FindYourCM_DB"];
const char *selectStmt = [selectSQL UTF8String];
if (sqlite3_prepare_v2(_FindYourMentorDB, selectStmt, -1, &statement, NULL) == SQLITE_OK) {
for (int recordRow = 0; sqlite3_step(statement) == SQLITE_ROW; recordRow++) {
NSString *studentLastName = [[NSString alloc]initWithUTF8String:(const char*)sqlite3_column_text(statement,0)];
NSString *studentFirstName = [[NSString alloc]initWithUTF8String:(const char*)sqlite3_column_text(statement, 1)];
NSString *studentID = [[NSString alloc]initWithUTF8String:(const char*)sqlite3_column_text(statement, 2)];
NSString *studentEmail = [[NSString alloc]initWithUTF8String:(const char*)sqlite3_column_text(statement, 3)];
NSString *programName = [[NSString alloc]initWithUTF8String:(const char*)sqlite3_column_text(statement, 4)];
NSString *majorityCampus = [[NSString alloc]initWithUTF8String:(const char*)sqlite3_column_text(statement, 5)];
NSString *discipline = [[NSString alloc]initWithUTF8String:(const char*)sqlite3_column_text(statement, 6)];
NSString *cmName = [[NSString alloc]initWithUTF8String:(const char*)sqlite3_column_text(statement, 7)];
NSString *cmID = [[NSString alloc]initWithUTF8String:(const char*)sqlite3_column_text(statement, 8)];
NSString *cmEmail = [[NSString alloc]initWithUTF8String:(const char*)sqlite3_column_text(statement, 9)];
NSString *cmPhone = [[NSString alloc]initWithUTF8String:(const char*)sqlite3_column_text(statement, 10)];
NSString *cmCampus = [[NSString alloc]initWithUTF8String:(const char*)sqlite3_column_text(statement, 11)];
NSString *studentNotes = [[NSString alloc]initWithUTF8String:(const char*)sqlite3_column_text(statement, 12)];
[[studentsInfo objectAtIndex:recordRow] addObject: [NSMutableArray arrayWithObjects:studentLastName, studentFirstName, studentID, studentEmail, programName, majorityCampus, discipline, cmName, cmID, cmEmail, cmPhone, cmCampus, studentNotes, nil]];
}
sqlite3_finalize(statement);
}
sqlite3_close(_FindYourMentorDB);
}
}
答案 0 :(得分:0)
主要问题是这一行:
[[studentsInfo objectAtIndex:recordRow] addObject: [NSMutableArray arrayWithObjects:studentLastName, studentFirstName, ...
由于studentsInfo
是一个新的空数组,因此无法从中访问它。您只想添加如下:
[studentsInfo addObject: [NSMutableArray arrayWithObjects:studentLastName, studentFirstName, ...
还有一些其他事情可以清理。
- (void) initializeDatabase {
// get the documents directory
NSArray *dirPaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *docsDir = dirPaths[0];
NSLog(@"Directory Path: %@", docsDir);
findYourMentorDB_Path = [docsDir stringByAppendingPathComponent:@"FindYourCM_Info.sqlite"];
NSMutableArray *studentsInfo = [NSMutableArray new];
if (sqlite3_open([findYourMentorDB_Path UTF8String], &_findYourMentorDB) == SQLITE_OK) {
NSString *selectSQL = @"SELECT StudentLastName, StudentFirstName, StudentID, StudentEmail, ProgramName, MajorityCampus, Discipline, CMName, CMID, CMEmail, CMPhone, CMCampus, StudentNotes FROM FindYourCM_DB";
const char *selectStmt = [selectSQL UTF8String];
sqlite3_stmt *statement;
if (sqlite3_prepare_v2(_findYourMentorDB, selectStmt, -1, &statement, NULL) == SQLITE_OK) {
while (sqlite3_step(statement) == SQLITE_ROW) {
NSString *studentLastName = [[NSString alloc]initWithUTF8String:(const char*)sqlite3_column_text(statement,0)];
NSString *studentFirstName = [[NSString alloc]initWithUTF8String:(const char*)sqlite3_column_text(statement, 1)];
NSString *studentID = [[NSString alloc]initWithUTF8String:(const char*)sqlite3_column_text(statement, 2)];
NSString *studentEmail = [[NSString alloc]initWithUTF8String:(const char*)sqlite3_column_text(statement, 3)];
NSString *programName = [[NSString alloc]initWithUTF8String:(const char*)sqlite3_column_text(statement, 4)];
NSString *majorityCampus = [[NSString alloc]initWithUTF8String:(const char*)sqlite3_column_text(statement, 5)];
NSString *discipline = [[NSString alloc]initWithUTF8String:(const char*)sqlite3_column_text(statement, 6)];
NSString *cmName = [[NSString alloc]initWithUTF8String:(const char*)sqlite3_column_text(statement, 7)];
NSString *cmID = [[NSString alloc]initWithUTF8String:(const char*)sqlite3_column_text(statement, 8)];
NSString *cmEmail = [[NSString alloc]initWithUTF8String:(const char*)sqlite3_column_text(statement, 9)];
NSString *cmPhone = [[NSString alloc]initWithUTF8String:(const char*)sqlite3_column_text(statement, 10)];
NSString *cmCampus = [[NSString alloc]initWithUTF8String:(const char*)sqlite3_column_text(statement, 11)];
NSString *studentNotes = [[NSString alloc]initWithUTF8String:(const char*)sqlite3_column_text(statement, 12)];
[studentsInfo addObject:@[ studentLastName, studentFirstName, studentID, studentEmail, programName, majorityCampus, discipline, cmName, cmID, cmEmail, cmPhone, cmCampus, studentNotes ]];
}
sqlite3_finalize(statement);
} else {
NSLog(@"Unable to prepare statement: %s", sqlite3_errmsg(_findYourMentorDB));
}
sqlite3_close(_findYourMentorDB);
} else {
NSLog(@"Unable to open database at %@", findYourMentorDB_Path);
}
}
的问题:
studentsInfo
数组做了什么?sqlite3_open
时默认情况下会创建该文件。答案 1 :(得分:0)
答案 2 :(得分:-1)
使用下一个方法:insertObject:yourarray atIndex:yourindex