0

大家好,我有一个应用程序可以将 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;
    }
}
}

请注意,此代码没有让我到达我要去的地方。

4

1 回答 1

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);
}
于 2012-09-20T15:49:15.800 回答