從sqlite3數據庫讀取時遇到一些問題。無法從sqlite3數據庫中選擇
-(void) readMessengesFromDatabase {
sqlite3 *database;
messenges = [[NSMutableArray alloc] init];
if(sqlite3_open([databasePath UTF8String], &database) == SQLITE_OK) {
NSLog(@"Connection OK");
const char *sqlStatement = "select * from MessagesData";
sqlite3_stmt *compiledStatement;
if(sqlite3_prepare_v2(database, sqlStatement, -1, &compiledStatement, NULL) == SQLITE_OK) NSLog(@"connect to table OK"); else NSLog(@"connect to table FALSE");
if(sqlite3_prepare_v2(database, sqlStatement, -1, &compiledStatement, NULL) == SQLITE_OK) { //не проходит условие
NSLog(@"Connection to table OK");
while(sqlite3_step(compiledStatement) == SQLITE_ROW) {
NSLog(@"Read rows OK");
NSString *dbMessageID = [NSString stringWithUTF8String:(char *)sqlite3_column_text(compiledStatement, 0)];
NSString *dbMessageText = [NSString stringWithUTF8String:(char *)sqlite3_column_text(compiledStatement, 1)];
NSString *dbMessageDate = [NSString stringWithUTF8String:(char *)sqlite3_column_text(compiledStatement, 2)];
NSString *dbMediaOrNot = [NSString stringWithUTF8String:(char *)sqlite3_column_text(compiledStatement, 3)];
Message *messege = [[Message alloc] initWithName:dbMessageText messageID:dbMessageID messageDate:dbMessageDate mediaOrNot:dbMediaOrNot];
[messenges addObject:messege];
[messege release];
}
}
sqlite3_finalize(compiledStatement);
}
sqlite3_close(database);
}
我的第一個NSLog告訴我什麼連接到數據庫是好的。但下一步「選擇*從MessagesData」和if(sqlite3_prepare_v2(database, sqlStatement, -1, &compiledStatement, NULL) == SQLITE_OK) NSLog(@"connect to table OK"); else NSLog(@"connect to table FALSE");
顯示我「連接到表FALSE」。嘗試從我的數據庫的表whith終端,並得到錯誤「無法打開數據庫文件」。我的錯誤在哪裏?我在代碼中看不到任何問題...
您選擇不使用核心數據的原因嗎? – wlangstroth 2012-04-09 11:44:39
或者至少FMDB? – Eugene 2012-04-09 11:49:34
我會很樂意使用核心數據,但我的編程水平很低,但尚未使用它:) – RomanHouse 2012-04-09 11:52:10