1

I have some problem deleting rows from a database and table.

The problem is: I can swipe the row, tap the "delete" button, but nothing happens.

Probably i'm doing it wrong.

Can Somebody give me a tip? Here there is all the program if you want to take a look:(updated)

http://cl.ly/9q7U

-(void)tableView:(UITableView *)_tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {

    if (editingStyle == UITableViewCellEditingStyleDelete) {

    NSDictionary *rowVals = (NSDictionary *) [shoppingListItems objectAtIndex:indexPath.row];
    NSString *keyValue = (NSString *) [rowVals objectForKey:@"key"];

    [_tableView beginUpdates];
    [_tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade];
    //Here i got the SIGABRT error
    [_tableView endUpdates];

    sqlite3 *db;
    int dbrc; //Codice di ritorno del database (database return code)
    DatabaseShoppingListAppDelegate *appDelegate = (DatabaseShoppingListAppDelegate*) [UIApplication sharedApplication].delegate;
    const char *dbFilePathUTF8 = [appDelegate.dbFilePath UTF8String];
    dbrc = sqlite3_open(dbFilePathUTF8, &db);
    if (dbrc) {
        NSLog(@"Impossibile aprire il Database!");
        return;
    }

    sqlite3_stmt *dbps; //Istruzione di preparazione del database

    NSString *deleteStatementsNS = [NSString stringWithFormat: @"DELETE FROM \"shoppinglist\" WHERE key='%@'", keyValue];

    const char *deleteStatement = [deleteStatementsNS UTF8String];
    dbrc = sqlite3_prepare_v2(db, deleteStatement, -1, &dbps, NULL);
    dbrc = sqlite3_step(dbps);


    //faccio pulizia rilasciando i database
    sqlite3_finalize(dbps);
    sqlite3_close(db);

}
}

#

Thanks to Akshay, finally i fixed this portion of code. I write here the solution for the ones who'll need it.

-(void)tableView:(UITableView *)_tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {

if (editingStyle == UITableViewCellEditingStyleDelete) {

    NSDictionary *rowVals = (NSDictionary *) [shoppingListItems objectAtIndex:indexPath.row];
    NSString *keyValue = (NSString *) [rowVals objectForKey:@"key"];

    [tableView beginUpdates];

    sqlite3 *db;
    int dbrc; //Codice di ritorno del database (database return code)
    DatabaseShoppingListAppDelegate *appDelegate = (DatabaseShoppingListAppDelegate*) [UIApplication sharedApplication].delegate;
    const char *dbFilePathUTF8 = [appDelegate.dbFilePath UTF8String];
    dbrc = sqlite3_open(dbFilePathUTF8, &db);
    if (dbrc) {
        NSLog(@"Impossibile aprire il Database!");
        return;
    }

    sqlite3_stmt *dbps; //Istruzione di preparazione del database

    NSString *deleteStatementsNS = [NSString stringWithFormat: @"DELETE FROM \"shoppinglist\" WHERE key='%@'", keyValue];

    const char *deleteStatement = [deleteStatementsNS UTF8String];
    dbrc = sqlite3_prepare_v2(db, deleteStatement, -1, &dbps, NULL);
    dbrc = sqlite3_step(dbps);


    //faccio pulizia rilasciando i database
    sqlite3_finalize(dbps);
    sqlite3_close(db);

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

    [tableView endUpdates];

    [tableView reloadData];
}
}
3
  • @Oiproks When asking a questions, please always tell us whether you have an error of some sorts, or you have an unexpected behavior and what the expected behavior is (which happens to be obvious in this case, but may not be obvious in general case). Commented Sep 4, 2011 at 10:27
  • Ok, sorry! I fixed the question. Commented Sep 4, 2011 at 10:30
  • @Oiproks Sorry for the interruption,my code is getting crashed at line NSString *keyValue = (NSString *) [rowVals objectForKey:@"key"];Please help me Commented Jan 9, 2012 at 12:00

2 Answers 2

2

You should be calling:

[tableView beginUpdates];
[tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath]
        withRowAnimation:UITableViewRowAnimationFade];
[tableView endUpdates];

inside commitEditingStyle: instead of reloadData. Also, make sure that you delete the entity from the DB & return one less from numberOfRowsInSection:.

Sign up to request clarification or add additional context in comments.

9 Comments

i tried as you said, but i receive SIGABRT on "[tableView endUpdates];", and I don't know how to return one less row... :( I'm not so skilled...
What do you return in numberOfRowsInSection: ? If you had 3 rows before swiping & were returning 3, now return 2.
There I return the number of items in the array shoppingListItems (return [shoppingListItems count];) charged from the db. How can I tell it to count one less?
Actually the program delete the content of the database. After i delete something, the simulator crash, i run it again, and the row is disappeared! So the problem is just in removing the row from the table!
The problem is the crash. You should delete the row from the DB between beginUpdates & endUpdates. If you are returning the right number after the swipe, you should be good.
|
1

I think the problem is in this line

NSString *insertStatementsNS = [NSString stringWithFormat: @"DELETE FROM \"shoppinglist\" WHERE key=?", indexPath];

I think you want to have in insertStatementsNS this:

NSString *insertStatementsNS = [NSString stringWithFormat: @"DELETE FROM \"shoppinglist\" WHERE key=%@", indexPath];

or

NSString *insertStatementsNS = [NSString stringWithFormat: @"DELETE FROM \"shoppinglist\" WHERE key='%@'", indexPath];

4 Comments

Well.. the problem is not only there! Actually i can't see if that one is working or not cos after I swipe and delete nothing happens... probably there is something wrong somewhere else :S
commitEditingStyle is calling ?
To read the correct value from the cell i did this. NSDictionary *rowVals = (NSDictionary *) [shoppingListItems objectAtIndex:indexPath.row]; NSString *keyValue = (NSString *) [rowVals objectForKey:@"key"]; Then i pass the keyValue variable to the Query. I'm sure is more efficient. But I still have problem deleting the row .____. that's so annoying! If I link you the project, can you take a look? maybe is something stupid...
Something new! Actually the program delete the content of te database. After i delete something, the simulator crash, i run again, and the row is disappeared! So the problem is just in removing the row from the table!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.