1
此函數中的所有內容似乎都按預期工作,只是沒有記錄實際顯示在數據庫表中。如果我通過Firefox sqlite管理器插件直接輸入sql,那麼記錄顯得很好。可能有些令人沮喪的愚蠢,任何想法?Sqlite插入語句不會在表中放入記錄
- (IBAction)buttonInsertRecord:(id)sender
{
// initializing db file access
NSFileManager *fileMgr = [NSFileManager defaultManager];
NSString *dbPath = [[[NSBundle mainBundle] resourcePath ]stringByAppendingPathComponent:@"KJV.sqlite"];
// testing to see if database was found
BOOL success = [fileMgr fileExistsAtPath:dbPath];
if(!success)
{
// database not found
labelStatus.text = @"database not found";
}
else
{
// database not found
labelStatus.text = @"database found";
}
sqlite3_stmt *statement;
sqlite3 *dbPointer;
if (sqlite3_open(dbPath.UTF8String, &dbPointer) == SQLITE_OK)
{
NSString *insertSQL = [NSString stringWithFormat: @"INSERT INTO users (user_name, user_id) VALUES ('testuser', null);"];
const char *insert_stmt = [insertSQL UTF8String];
sqlite3_prepare_v2(dbPointer, insert_stmt, -1, &statement, NULL);
if (sqlite3_step(statement) == SQLITE_DONE)
{
labelStatus.text = @"added";
}
else
{
labelStatus.text = @"failed";
}
sqlite3_finalize(statement);
sqlite3_close(dbPointer);
}
}
這個問題經常出現(儘管此刻我找不到重複)。您無法寫入只讀資源包(在真實設備上)。您需要將數據庫複製到應用程序沙箱的適當可寫區域,然後使用該數據庫的副本。 – rmaddy