Actualizar la base de datos Sqlite solo funciona una vez

I have an sqlite database and it's loaded into a tableview, now when i click a row to see its details..i go to another view controller in which details are displayed and a AddToFav button exists. When i click it..and go to another view controller...i can see the changes correctly, my database is updating correctly, but if i try to add another entry..i notice that my database is not updated anymore..The addToFav button only updates the database once. Any ideas? This is my 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) {
                //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");

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


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

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

                if(/* == @"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;



                    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;

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

         //       [Favo release];

         //       NSLog(Favo);

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

preguntado el 24 de agosto de 12 a las 07:08

You open and close the database everytime you do something with it? -

how do you suggest doing it ? I'm a newb in xcode -

Holding the sqlite database handle in the app delegate and only closing when the app goes into the background. In other database architectures, connecting to the database is extremely expensive... -

can you provide a sample or documentation on this matter? -

No not really. The best thing to do is create wrapper classes for SqliteDatabase y SqliteStatement that make it easy to manage the lifetime of those objects and you could easily just pass the SqliteDatabase object to each new object you create. -

1 Respuestas

Thank you @crazyCreator for your answer !!!!

[self authorList2];
[self.tableView recargar datos];

Respondido el 24 de Septiembre de 12 a las 07:09

No es la respuesta que estás buscando? Examinar otras preguntas etiquetadas or haz tu propia pregunta.