从表和sqlite数据库中删除行 [英] Delete row from table and sqlite database

查看:147
本文介绍了从表和sqlite数据库中删除行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我仍然需要你的帮助。

我有这段代码不想工作。

I still need your help.
I have this piece of code that doesn't want to work.

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

    [tableView endUpdates];
}
}

实际上,它工作正常。我可以在表中滑动到一行,删除它,我有淡入淡出效果,内容从数据库和表中删除。

但是如果我尝试在第一个之后删除另一个元素我有一个SIGABRT

Actually, it's working fine. I can swipe to a row in the table, delete it, i have my fade effect, the content is removed from the database and from the table.
But if I try to remove another element just after the first one I have a SIGABRT on

    [shoppingListItems removeObjectAtIndex:indexPath.row];

如果我移动到另一个标签,请返回并删除一行,一切正常...... < br>
有什么想法吗?

If I move to another tab, go back and remove a row, everything works fine...
Any ideas?

推荐答案

我希望shoppingListItems数组包含Dictinaries。所以,对象在数组中你可以直接从数组中删除该特定对象。
尝试喜欢这个......

i hope shoppingListItems array contains Dictinaries . so have that Object In Array u can Directly Delete that particular object From Array. TRY LIKE THIS.....

  -(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"];
    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);
    [shoppingListItems removeObjectAtIndex:indexPath.row];
    [tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade];

    [tableView reloadData];
}
}

这篇关于从表和sqlite数据库中删除行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆