2013-12-22 169 views
1

我想更新行中的一個單元格,但我無法獲得它的工作。對於更新方法:sqlite中的更新行沒有更新

- (void) UpdateQuestionShownParameter:(int)QuestionId :(BOOL)QuestionShown{ 
    @try { 
     NSFileManager *fileMgr = [NSFileManager defaultManager]; 
     NSString *dbPath = [[[NSBundle mainBundle] resourcePath ]stringByAppendingPathComponent:@"Milionar.sqlite"]; 
     const char *sql = "UPDATE Questions set Show = ? WHERE id = ?"; 


     BOOL success = [fileMgr fileExistsAtPath:dbPath]; 
     if(!success) 
     { 
      NSLog(@"Cannot locate database file '%@'.", dbPath); 
     } 
     if(sqlite3_open([dbPath UTF8String], &db) == SQLITE_OK) 
     { 
      sqlite3_stmt *sqlStatement; 
      if(sqlite3_prepare_v2(db, sql, -1, &sqlStatement, NULL) == SQLITE_OK) 
      { 
       NSInteger shownInteger = (QuestionShown ? 1 : 0); 

       sqlite3_bind_int(sqlStatement, 1, shownInteger); 
       sqlite3_bind_int(sqlStatement, 2, QuestionId); 

       if (sqlite3_step(sqlStatement) != SQLITE_DONE) 
       { 
        NSLog(@"Error while updating. '%s'", sqlite3_errmsg(db)); 
       } 
       sqlite3_finalize(sqlStatement); 

      } 
      else 
      { 
       NSLog(@"Problem with prepare statement"); 
      } 
     } 
     else 
     { 
      NSLog(@"An error has occured while opening database."); 
     } 

     sqlite3_close(db); 

    } 
    @catch (NSException *exception) { 
     NSLog(@"An exception occured: %@", [exception reason]); 
    } 
} 

嘗試在viewDidLoad中:

- (void)viewDidLoad 
{ 
    ListOfQuestions *listQuestions =[[ListOfQuestions alloc] init]; 
    self.Questions = [listQuestions getQuestions]; 
    Question *generatedQuestion = (Question *) [self.Questions objectAtIndex:0]; 
    [listQuestions UpdateQuestionShownParameter:generatedQuestion.id :TRUE]; 
    [self.Description setText:(generatedQuestion.Description)]; 

    [super viewDidLoad]; 
    // Do any additional setup after loading the view. 
} 

每次當我試圖運行應用程序,我在所示列得到0。但我沒有任何錯誤。所以,當我嘗試在模擬器中運行應用程序時,我做了一些錯誤或每次都出現的問題我從項目數據庫中重新創建數據庫? 謝謝

回答

3

您正在打開只讀數據庫中的數據庫。你應該複製數據庫從包到文檔如果數據庫不已經在文檔文件夾中的文件夾:

NSString *filename   = @"Milionar.sqlite"; 
NSFileManager *fileManager = [NSFileManager defaultManager]; 
NSString *bundlePath  = [[[NSBundle mainBundle] resourcePath ]stringByAppendingPathComponent:filename]; 
NSString *documentsFolder = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES)[0]; 
NSString *documentsPath = [documentsFolder stringByAppendingPathComponent:filename]; 

if (![fileManager fileExistsAtPath:documentsPath]) { 
    NSError *error = nil; 
    BOOL success = [fileManager copyItemAtPath:bundlePath toPath:documentsPath error:&error]; 
    NSAssert(success, @"Unable to copy database: %@", error); 
} 

if (sqlite3_open([documentsPath UTF8String], &db) != SQLITE_OK) { 
    NSLog(@"Open failed"); 
} else { 
    // ... 
} 

更多有關文件所在所屬看到File System Programming Guide


順便說一句,如果你正在尋找你的模擬器的文檔文件夾,這是位於~/Library/Application Support/iPhone Simulator(在Xcode 6,這是目前~/Library/Developer/CoreSimulator/Devices)。如果沒有看到「庫」文件夾,您可以通過鍵入以下命令到您的終端命令行界面取消隱藏:

 
chflags nohidden ~/Library 
+0

正是我一直在尋找,並與鏈接的文章,並與偉大的小費。謝謝++ –