从表和sqlite数据库中删除行。

时间:2022-06-01 21:27:38

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

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.

感谢Akshay,我最终修复了这部分代码。我把需要它的解写在这里。

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

2 个解决方案

#1


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:.

commitEditingStyle内部:而不是reloadData。另外,请确保您从数据库中删除了实体,从numberOfRowsInSection中减少了一个。

#2


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:

我认为你想在insertStatementsNS中

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

or

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

#1


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:.

commitEditingStyle内部:而不是reloadData。另外,请确保您从数据库中删除了实体,从numberOfRowsInSection中减少了一个。

#2


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:

我认为你想在insertStatementsNS中

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

or

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