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.