从表和sqlite数据库删除行
问题描述:
我有一些问题,从数据库和表中删除行。从表和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];
[_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);
}
}
#
感谢阿克沙伊,最后我固定的代码的这一部分。我在这里给那些需要它的人写解决方案。
-(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];
}
}
答
你应该打电话:
[tableView beginUpdates];
[tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath]
withRowAnimation:UITableViewRowAnimationFade];
[tableView endUpdates];
里面的commitEditingStyle:
而不是reloadData
。另外,请确保从DB &中删除实体,从numberOfRowsInSection:
返回一个较少的实体。
答
我认为这个问题是在这条线
NSString *insertStatementsNS = [NSString stringWithFormat: @"DELETE FROM \"shoppinglist\" WHERE key=?", indexPath];
我想你想在insertStatementsNS
此:
NSString *insertStatementsNS = [NSString stringWithFormat: @"DELETE FROM \"shoppinglist\" WHERE key=%@", indexPath];
或
NSString *insertStatementsNS = [NSString stringWithFormat: @"DELETE FROM \"shoppinglist\" WHERE key='%@'", indexPath];
@Oiproks在提问时,请始终告诉我们您是否有某种错误,或者您有意外的行为,以及期望的行为是什么(这种情况在这种情况下很明显,但可能并不明显一般情况下)。 –
好的,对不起!我解决了这个问题。 – Oiproks
@Oiproks对不起,我的代码在NSString * keyValue =(NSString *)[rowVals objectForKey:@“key”]处崩溃了;请帮助我 –