2013-12-22 3 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에 위치하고 (엑스 코드 6,이 ~/Library/Developer/CoreSimulator/Devices 지금이다). 당신은 "라이브러리"폴더가 표시되지 않는 경우, 당신은 당신의 터미널 명령 줄 인터페이스에 다음 명령을 입력하여 숨기기를 취소 할 수 있습니다

 
chflags nohidden ~/Library 
+0

을 나는과 기사 링크와 함께 큰 팁을 찾고 정확히 무엇인지. 고마워요 ++ –

관련 문제