gpt4 book ai didi

iphone - 从表和SQLite数据库中删除行

转载 作者:行者123 更新时间:2023-12-03 18:39:58 25 4
gpt4 key购买 nike

我仍然需要你的帮助。
我有这段代码不想工作。

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


如果我移至另一个标签,请返回并删除一行,一切正常...
有任何想法吗?

最佳答案

我希望shoppingListItems数组包含Dictionaries。因此,该对象可以直接从数组中删除该特定对象。
尝试一下.....

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

关于iphone - 从表和SQLite数据库中删除行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7306309/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com