2012-12-21 104 views
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); 
    } 
} 
+0

這個問題經常出現(儘管此刻我找不到重複)。您無法寫入只讀資源包(在真實設備上)。您需要將數據庫複製到應用程序沙箱的適當可寫區域,然後使用該數據庫的副本。 – rmaddy

回答

4
//your database is not writable because its in the bundle. 
    NSString *dbPath = [[[NSBundle mainBundle] resourcePath ]stringByAppendingPathComponent:@"KJV.sqlite"]; 

如果你想有一個可讀/寫的數據庫,你可以這樣做:

- (void) createEditableCopyOfDatabaseIfNeeded 
    { 
     BOOL success; 
     NSFileManager *fileManager = [NSFileManager defaultManager]; 
     NSError *error; 
     NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); 
     NSString *documentsDirectory = [paths objectAtIndex:0]; 
     NSString *writableDBPath = [documentsDirectory stringByAppendingPathComponent:@"databasename.sqlite"]; 
     NSLog(@"writableDBPath == %@",writableDBPath); 
     if ([fileManager fileExistsAtPath:writableDBPath]) 
     { 
      NSLog(@"Database already exist"); 
     } 
     else 
     { 
      NSString *defaultDBPath = [[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:@"databasename.sqlite"]; 
      success = [fileManager copyItemAtPath:defaultDBPath toPath:writableDBPath error:&error]; 
      if (!success) 
       NSLog(@"Failed to create writable database file with message '%@'.", [error localizedDescription]); 
     } 
    } 

和繼承人如何添加數據

- (void) addToDatabase 
{ 
     NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); 
     NSString *documentsDirectory = [paths objectAtIndex:0]; 
     NSString *databasePath = [documentsDirectory stringByAppendingPathComponent:@"databasename.sqlite"]; 

     if(sqlite3_open([databasePath UTF8String], &mDatabase_update) == SQLITE_OK) 
     { 

      NSString *sqlQuery = [NSString stringWithFormat: @"Insert into table 
      . 
      . 
      . 
      . 
+0

感謝您的回答。我不知道你不能寫入捆綁的數據庫,儘管回想起來我認爲它是有道理的。所以,基本上我需要創建一個新的數據庫,當應用程序首次在本地手機上運行時,所有'volatile'正在進行的用戶數據和該數據庫將在那裏,直到該應用程序被卸載?再次感謝您花時間回答。 – Grymjack