Hy all, 我有一个应用程序将数据从sqlite数据库加载到表视图中。 在此视图中显示的字段是在sqlite数据库中将字段集设置为值= YES的条目。因此,当应用程序加载时,它将自动检查数据库并加载具有值的所有字段:Fav = YES。这就是为什么,我需要当我滑动删除时,条目消失,但另外我需要它来更改相应数据库中字段的值并将其设置为Fav = NO。这是我的代码片段,希望有人能帮帮我! FavReal是类名
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath {
return YES;
}
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
if(editingStyle == UITableViewCellEditingStyleDelete)
{
@try {
sqlite3_stmt *compiled_statement10;
if(sqlite3_open([PathDB UTF8String], &db2) == SQLITE_OK) {
FavReal *SelectedFav;
//remove the deleted object from your data source.
//If you're data source is an NSMutableArray, do this
NSString *formatedSql2 = [NSString stringWithFormat:@"UPDATE Sheet1 SET Fav = 'NO' WHERE field3 = '%@'" , SelectedFav.description ];
const char *sql = [formatedSql2 UTF8String];
int success2 = sqlite3_step(compiled_statement10);
// sqlite3_reset(compiled_statement1);
if (success2 != SQLITE_ERROR) {
NSLog(@"Successfully deleted");
sqlite3_last_insert_rowid(db2);
}
if (sqlite3_prepare_v2(db2, sql, -1, &compiled_statement10, NULL) != SQLITE_OK) {
NSLog(@"!!!!!!!!!!!!!!!!!!!ERRRRROOOOOOORRRRRRRRR!!!!!!!!!!!!!!!!!!!!!!!!!");
//NSAssert1(0, @"Error: failed to prepare statement with message '%s'.", sqlite3_errmsg(database));
}
int success = sqlite3_step(compiled_statement10);
// sqlite3_reset(compiled_statement1);
if (success != SQLITE_ERROR) {
NSLog(@"Successfully deleted the Favorite item named : %@",SelectedFav.description);
sqlite3_last_insert_rowid(db2);
}
[self.theFav removeObjectAtIndex:indexPath.row];
[self.tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade];
}}
@catch (NSException *exception) {
NSLog(@"Problem with prepare statement: %@", sqlite3_errmsg(db2));
}
@finally {
// sqlite3_finalize(sqlStatement);
sqlite3_close(db2);
return theFav;
}
}
}
请注意,这段代码不能让我到达目的地。
答案 0 :(得分:0)
尝试这样的事情:
if (sqlite3_open(..., &db2) != SQLITE_OK) {
return error;
}
@try {
sqlite3_stmt *update_statement;
const char *sql = ...;
if (sqlite3_prepare_v2(db2, sql, -1, &update_statement, NULL) != SQLITE_OK) {
return error;
}
@try {
int success = sqlite3_step(update_statement);
if (success != SQLITE_DONE) {
return error;
}
}
@finally {
sqlite3_finalize(update_statement);
}
@finally {
sqlite3_close(db2);
}