0

Hy all, I have an application that loads data from an sqlite database into a table view. Fields that shows up in this view are entries having a field set in the sqlite database to a value = YES. So when the application loads, it will automatically checks the database and loads all the fields having the value : Fav = YES. That's why, i need when i swipe to delete, the entry disappears, but in addition i need it to change the value of the field in the appropriate database and set it to Fav = NO. Here is my code snippet and hope someone could help me out!
FavReal is the class name

- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath {
    return YES;
}
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
    if(editingStyle == UITableViewCellEditingStyleDelete)
    {

        @try {

        sqlite3_stmt *compiled_statement10;

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

            FavReal *SelectedFav;

            //remove the deleted object from your data source.
            //If you're data source is an NSMutableArray, do this
            NSString *formatedSql2 = [NSString stringWithFormat:@"UPDATE Sheet1 SET Fav = 'NO' WHERE field3 = '%@'" , SelectedFav.description ];
            const char *sql = [formatedSql2 UTF8String];
            int success2 = sqlite3_step(compiled_statement10);

            // sqlite3_reset(compiled_statement1);
            if (success2 != SQLITE_ERROR) {
                NSLog(@"Successfully deleted");
                sqlite3_last_insert_rowid(db2);
            }



            if (sqlite3_prepare_v2(db2, sql, -1, &compiled_statement10, NULL) != SQLITE_OK) {
                NSLog(@"!!!!!!!!!!!!!!!!!!!ERRRRROOOOOOORRRRRRRRR!!!!!!!!!!!!!!!!!!!!!!!!!");
                //NSAssert1(0, @"Error: failed to prepare statement with message '%s'.", sqlite3_errmsg(database));
            }
            int success = sqlite3_step(compiled_statement10);

            // sqlite3_reset(compiled_statement1);
            if (success != SQLITE_ERROR) {
                NSLog(@"Successfully deleted the Favorite item named : %@",SelectedFav.description);
                sqlite3_last_insert_rowid(db2);

            }



            [self.theFav removeObjectAtIndex:indexPath.row];
            [self.tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade];

        }}



    @catch (NSException *exception) {

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

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

        return theFav;
    }
}
}

Note that this code DID NOT get me where i'm going.

Elias Rahme
  • 2,226
  • 3
  • 28
  • 53
  • You told that you want to delete the entry when the cell is swiped and you are using UPDATE query, how do you think that it will work? – Charan Sep 20 '12 at 13:22
  • Update the corresponding field in the database!!!! i want to remove the entry from the view, but still have it in the database...just change the value of a field in the databnase – Elias Rahme Sep 20 '12 at 13:38

1 Answers1

0

Try something like this:

if (sqlite3_open(..., &db2) != SQLITE_OK) {
    return error;
}
@try {
    sqlite3_stmt *update_statement;
    const char *sql = ...;
    if (sqlite3_prepare_v2(db2, sql, -1, &update_statement, NULL) != SQLITE_OK) {
        return error;
    }
    @try {
        int success = sqlite3_step(update_statement);
        if (success != SQLITE_DONE) {
            return error;
        }
    }
    @finally {
        sqlite3_finalize(update_statement);
    }
@finally {
    sqlite3_close(db2);
}
CL.
  • 173,858
  • 17
  • 217
  • 259