2012-08-24 3 views
0

sqlite 데이터베이스가 있고 tableview에로드되었습니다. 이제 행을 클릭하여 세부 정보를 볼 수 있습니다 .i 세부 정보가 표시되고 AddToFav 버튼이있는 다른보기 컨트롤러로 이동합니다. 내가 그것을 클릭하고 다른보기 컨트롤러로 이동하면 ... 변경 사항을 올바르게 볼 수 있지만 데이터베이스가 올바르게 업데이트되고 있지만 다른 항목을 추가하려고하면 데이터베이스가 더 이상 업데이트되지 않습니다. addToFav 버튼은 데이터베이스를 한 번만 업데이트합니다. 어떤 아이디어? 이 내 Addbut IBAction를 수 있습니다 :업데이트 Sqlite 데이터베이스는 한 번만 작동합니다.

- (IBAction)AddButClick:(UIButton *)sender { 



    [AddBut setImage:[UIImage imageNamed:@"apple-logo copy.png"] forState:UIControlStateSelected]; 
    [AddBut setImage:[UIImage imageNamed:@"apple-logo copy.png"] forState:UIControlStateHighlighted]; 
    Favo = [[NSMutableArray alloc] initWithCapacity:1000000]; 



    @try { 
     // NSFileManager *fileMgr = [NSFileManager defaultManager]; 
     // NSString *dbPath = [[[NSBundle mainBundle] resourcePath ]stringByAppendingPathComponent:@"dictionary.sqlite"]; 
     //NSString *dbPath = [[[NSBundle mainBundle] resourcePath ]stringByAppendingPathComponent:@"authorsDb2.sqlite"]; 
     //  NSString *dbPath = [[[NSBundle mainBundle] resourcePath ]stringByAppendingPathComponent:@"FinalDb.sqlite"]; 
     //NSString *dbPath = [[[NSBundle mainBundle] resourcePath ]stringByAppendingPathComponent:@"xxJuridique-FINAL-OK.sqlite"]; 
    // [self createEditableCopyOfDatabaseIfNeeded]; 


    //  NSString *dbPath = [[[NSBundle mainBundle] resourcePath ]stringByAppendingPathComponent:@"data.sqlite"]; 
     // [self.tableView reloadData]; 

     sqlite3_stmt *compiled_statement1; 

     if(sqlite3_open([PathDB UTF8String], &db) == SQLITE_OK) { 



//   UIAlertView *DbO = [[UIAlertView alloc] 
//         initWithTitle:@"PATH" message:@"Database still open !" delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil]; 
//    
//   [DbO show]; 
      NSString *formatedSql = [NSString stringWithFormat:@"UPDATE Sheet1 SET Fav = 'YES' WHERE field3 = '%@'" , authorNAme2]; 

//   UIAlertView *messageAlert = [[UIAlertView alloc] 
//           initWithTitle:@"Query" message:formatedSql delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil]; 
//    
//   [messageAlert show]; 
//    
      UIAlertView *PAth = [[UIAlertView alloc] 
           initWithTitle:@"PATH" message:PathDB delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil]; 

      [PAth show]; 
      // NSLog(PathDB); 

      const char *sql = [formatedSql UTF8String]; 
      NSLog(@" !!!!!!!! In the middle of it !!!!!!!!!!!!"); 
      if (sqlite3_prepare_v2(db, sql, -1, &compiled_statement1, NULL) != SQLITE_OK) { 
       NSLog(@"!!!!!!!!!!!!!!!!!!!ERRRRROOOOOOORRRRRRRRR!!!!!!!!!!!!!!!!!!!!!!!!!"); 
       //NSAssert1(0, @"Error: failed to prepare statement with message '%s'.", sqlite3_errmsg(database)); 
      } 

      NSLog(@"This is the query %@",formatedSql); 

     } 
    //  sqlite3_bind_text(compiled_statement1, 1, [authorNAme2 UTF8String], -1, SQLITE_TRANSIENT); 


     int success = sqlite3_step(compiled_statement1); 

     // sqlite3_reset(compiled_statement1); 
     if (success != SQLITE_ERROR) { 
      NSLog(@"Successfully inserted"); 
      sqlite3_last_insert_rowid(db); 
     } 

//   
//  BOOL success = [fileMgr fileExistsAtPath:dbPath]; 
//   
//  if(!success) 
//  { 
//   NSLog(@"Cannot locate database file '%@'.", dbPath); 
//  } 
//  if(!(sqlite3_open([dbPath UTF8String], &db) == SQLITE_OK)) 
//  { 
//   NSLog(@"An error has occured: %@", sqlite3_errmsg(db)); 
//    
//  } 


     // const char *sql = "SELECT F_Keyword FROM wordss"; 
     const char *sql = "SELECT * FROM Sheet1"; 
     NSLog(@"Successfully selected from database"); 
     sqlite3_stmt *sqlStatement; 
     if(sqlite3_prepare(db, sql, -1, &sqlStatement, NULL) != SQLITE_OK) 
     { 
      NSLog(@"Problem with prepare statement: %@", sqlite3_errmsg(db)); 


     }else{ 


      NSLog(@"Got in the else tag"); 

      while (sqlite3_step(sqlStatement)==SQLITE_ROW /*&& PAss == NO*/) { 

       NSLog(@"Got in the while tag"); 

       Author * author = [[Author alloc] init]; 
       NSLog(@"Author initialized"); 

       author.name = [NSString stringWithUTF8String:(char *) sqlite3_column_text(sqlStatement,10)]; 
       NSLog(@"Initialization ok"); 
      //  NSLog(author.name); 

       if(/*author.name == @"NO" &&*/ HighLighted == NO){ 
        //const char *sql2 = "INSERT INTO Sheet1 "; 

        [AddBut setImage:[UIImage imageNamed:@"apple-logo copy.png"] forState:UIControlStateNormal]; 
        NSLog(@"We have not selected it as fav yet"); 
        // [AddBut setSelected:NO]; //btn changes to normal state 
        NSLog(@"The button was NOt highlighted and now is"); 
        HighLighted = YES; 
        break; 

       } 

       else 
       { 

        NSLog(@"We have selected it as fav"); 

        [AddBut setImage:[UIImage imageNamed:@"apple-logo.png"] forState:UIControlStateNormal]; 
        [AddBut setSelected:NO]; //btn changes to normal state 
        NSLog(@"The button was highlighted and now is NOt"); 


        HighLighted = NO; 
        break; 

        // [self viewDidLoad]; 
        // PAss = YES; 

       } 
     //  [Favo release]; 

     //  NSLog(Favo); 

//    author.name = [NSString stringWithUTF8String:(char *) sqlite3_column_text(sqlStatement,2)]; 
//    author.title = [NSString stringWithUTF8String:(char *) sqlite3_column_text(sqlStatement,2)]; 
//    author.genre = [NSString stringWithUTF8String:(char *) sqlite3_column_text(sqlStatement, 4)]; 
//    [theauthors addObject:author]; 
      } 
     } 
    } 
    @catch (NSException *exception) { 

     NSLog(@"Problem with prepare statement: %@", sqlite3_errmsg(db)); 

    } 
    @finally { 
     // sqlite3_finalize(sqlStatement); 
    // sqlite3_close(db); 

     UIAlertView *fin = [[UIAlertView alloc] 
               initWithTitle:@"Query" message:@"DOne with Click Button" delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil]; 

        [fin show]; 

     return Favo; 
    } 
+0

데이터베이스를 열고 작업 할 때마다 데이터베이스를 열고 닫으십니까? – trojanfoe

+0

어떻게 할 건가요? xcode의 newb입니다. –

+0

응용 프로그램 대리인에서 sqlite 데이터베이스 핸들을 잡고 앱이 백그라운드로 들어갈 때만 닫습니다. 다른 데이터베이스 아키텍처에서 데이터베이스에 연결하는 것은 매우 비쌉니다 ... – trojanfoe

답변

0

당신 @crazyCreator 답변에 대한 감사 !!!!

[self authorList2];
[self.tableView reloadData];

관련 문제