从表和 sqlite 数据库中删除行
我仍然需要你的帮助。
我有这段代码不想工作。
-(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
[shoppingListItems removeObjectAtIndex:indexPath.row];
如果我移动到另一个选项卡,返回并删除一行,一切都会正常...
有什么想法吗?
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
[shoppingListItems removeObjectAtIndex:indexPath.row];
If I move to another tab, go back and remove a row, everything works fine...
Any ideas?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我希望 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.....
你可能想交换
做到这一点
或者,请告诉我们 SIGABRT 的错误消息是什么。
You might want to swap around
to make it
Alternatively please tell us what the error message for the SIGABRT is.