我在iOS應用程序中讀取數據庫中的某些數據。該功能是非常簡單的:iOS Sqlite3 SQLITE_ERROR
-(void) readCategories {
sqlite3 *database;
if([[NSFileManager defaultManager] fileExistsAtPath:databasePath]){
if(sqlite3_open([databasePath UTF8String], &database) == SQLITE_OK) {
const char *sqlStatement = "SELECT * FROM Categories ORDER BY name COLLATE NOCASE ASC";
sqlite3_stmt *compiledStatement;
int errorCode = sqlite3_prepare_v2(database, sqlStatement, -1, &compiledStatement, NULL);
if(errorCode == SQLITE_OK) {
while(sqlite3_step(compiledStatement) == SQLITE_ROW) {
[categories addObject:[NSString stringWithUTF8String:(char *)sqlite3_column_text(compiledStatement, 1)]];
}
} else {
NSLog(@"Error reading categories. Code: %d, message: '%s'", errorCode,sqlite3_errmsg(database));
}
sqlite3_finalize(compiledStatement);
sqlite3_close(database);
} else {
NSLog(@"Error opening DB");
}
} else {
NSLog(@"DB does not exist.");
}
}
的問題是,錯誤碼爲始終SQLITE_ERROR根據文檔是:「SQL錯誤或丟失的數據庫」。給出的消息是:'沒有這樣的表:類別'
現在,如果我查看我的計算機上的數據庫文件,表格顯然存在。我也可以在其上運行完全相同的查詢,並且它可以正常工作。
有沒有人有什麼想法出錯?
謝謝。
您確定databasePath中的文件是正確的數據庫文件嗎? –
你應該使用sqlite3_errmsg(http://sqlite.org/c3ref/errcode.html)來查看出了什麼問題。 SQLITE_ERROR也意味着你的SQL是錯誤的。 – Stefan
我已編輯我的帖子,更多信息。 –