xcode 从表和sqlite数据库中删除行
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/7306309/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Delete row from table and sqlite database
提问by Oiproks
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];
}
}
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
实际上,它运行良好。我可以滑动到表格中的一行,删除它,我有我的淡入淡出效果,内容从数据库和表格中删除。
但是,如果我尝试在第一个元素之后删除另一个元素,则我有一个 SIGABRT
[shoppingListItems removeObjectAtIndex:indexPath.row];
If I move to another tab, go back and remove a row, everything works fine...
Any ideas?
如果我移动到另一个选项卡,返回并删除一行,一切正常......有
任何想法吗?
回答by Srinivas
i hope shoppingListItems array contains Dictinaries . so have that Object In Array u can Directly Delete that particular object From Array. TRY LIKE THIS.....
我希望shoppingListItems 数组包含Dictinaries。所以有那个对象在数组中,你可以直接从数组中删除那个特定的对象。试试这个.....
-(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];
}
}
回答by Olaf
You might want to swap around
你可能想交换
[tableView reloadData]; [tableView endUpdates];
[tableView reloadData]; [tableView endUpdates];
to make it
使它
[tableView endUpdates]; [tableView reloadData];
[tableView endUpdates]; [tableView reloadData];
Alternatively please tell us what the error message for the SIGABRT is.
或者,请告诉我们 SIGABRT 的错误消息是什么。