更新Sqlite数据库只能工作一次

时间:2012-08-24 06:22:55

标签: sqlite sql-update xcode4.3

我有一个sqlite数据库,它被加载到tableview中,现在当我点击一行查看其详细信息时......我转到另一个视图控制器,其中显示了详细信息并且存在AddToFav按钮。 当我点击它...并转到另一个视图控制器...我可以正确看到更改,我的数据库正确更新,但如果我尝试添加另一个条目..我注意到我的数据库不再更新.. addToFav按钮仅更新数据库一次。有任何想法吗? 这是我的Addbut IBaction:

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

1 个答案:

答案 0 :(得分:0)

谢谢@crazyCreator的回答!!!!

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