2012-08-17 75 views
0

我有一個保存按鈕的應用程序。當我點擊保存按鈕時,應用程序應該自動更新一行的值並將其設置爲YES。使用Xcode更新Sqlite數據庫的字段值

直到現在我有以下代碼,但它實際上並沒有工作。請記住,我的應用程序是一個標籤菜單應用程序。在第一個視圖中,我有表格單元格,點擊時我轉到另一個視圖控制器,一個詳細視圖控制器,其中存在按鈕。

當我點擊按鈕時,數據庫中'Fav'字段的值應該從NO更改爲YES。

這是我的代碼:

- (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]; 

    // NSLog(authorNAme); 

    @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"]; 



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

     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)); 

     } 



     sqlite3_stmt *compiled_statement1; 
     if(sqlite3_open([dbPath UTF8String], &db) == SQLITE_OK) { 
      //const char *sqlStatement = 
      NSString *formatedSql = [NSString stringWithFormat:@"UPDATE Sheet1 SET Fav = 'YES' WHERE field3 = '%@' " , authorNAme2]; 

      NSLog(@"This is the query %@",formatedSql); 
      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(db)); 
      } 
     // sqlite3_exec(db, [compiled_statement1 UTF8String], NULL, NULL, NULL); 
//sqlite3_bind_text(compiled_statement1, 1, [formatedSql 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); 
     } 
      sqlite3_finalize(compiled_statement1); 


     } 

     //   
//  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; 







        // PAss = YES; 
        // [self release]; 
        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); 

     return Favo; 
    } 
+0

看看這個http://stackoverflow.com/questions/7169966/sqlite-database-update – Tendulkar 2012-08-17 08:48:07

+0

我確實看過它,但試圖將它與我自己的代碼造成了一個很大的問題,根本無法工作:S可以幫助整合嗎?謝謝先生 – 2012-08-17 09:18:21

+0

首先你必須複製數據庫到Documents目錄使用這兩個函數 – Tendulkar 2012-08-17 09:19:58

回答

1

的問題是不是在添加,但點擊,它實際上是在viewWillAppear中,其中我應該做的:

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

爲了加載可變數組,然後重新加載數據