Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

I am trying to update one of my table's column upon button click.

The issue is that code isn't giving any exception still it is not updating the table.

When the view controller load again , it shows the old value rather then updated one.

Code for update db is attached below.

@try {
    if(sqlite3_open([databasePath UTF8String], &database) == SQLITE_OK) {

        NSString *sqlStatement = @"UPDATE messagesTable SET Favorite = 1 where MessageID=";
        sqlStatement = [sqlStatement stringByAppendingString:msgId];

        NSLog(@"sql statement: %@", sqlStatement);



        const char *sql = [sqlStatement UTF8String];
        int result = sqlite3_prepare_v2(database, sql, -1, &compiledStatement, NULL);
        if(result != SQLITE_OK) {
            NSLog(@"Prepare-error #%i: %s", result, sqlite3_errmsg(database));
        }

                   // Release the compiled statement from memory
        sqlite3_finalize(compiledStatement);

    }


    sqlite3_close(database);


}
@catch (NSException *exception) {
    NSLog(@"Name: %@",exception.name);
    NSLog(@"Reason: %@",exception.reason);

}
@finally {

}

Any suggestions are welcome. :-)

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
902 views
Welcome To Ask or Share your Answers For Others

1 Answer

You are not calling sqlite3_step, which performs the update. After the sqlite3_prepare and before the sqlite3_finalize, you need something like:

if (sqlite3_step(compiledStatement) != SQLITE_DONE)
    NSLog(@"%s: step error: %s", __FUNCTION__, sqlite3_errmsg(database));

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...