2012-08-24 45 views
0

我有一個SQLite數據庫和它裝入的tableview,現在,當我點擊一行來看看它的details..i去其中的細節顯示和AddToFav按鈕存在另一個視圖控制器。 當我點擊it..and去到另一個視圖控制器......我能看到的變化正確的,我的數據庫正確更新,但如果我嘗試添加其他entry..i通知我的數據庫沒有更新了.. 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中的新手 –

+0

在應用程序委託中持有sqlite數據庫句柄,並且僅在應用程序進入後臺時關閉。在其他數據庫體系結構中,連接到數據庫是非常昂貴的... – trojanfoe

回答

0

謝謝@crazyCreator爲您的答案!

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