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 に答える