Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
931 views
in Technique[技术] by (71.8m points)

database - Sqlite3 update query not working in ios app

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

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

1 Reply

0 votes
by (71.8m points)

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));

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...