Sqlite3更新查询不工作在iOS应用程序

我试图更新我的表的列之一button点击。

问题是代码没有给出任何exception,但它不更新表。

当视图控制器再次加载时,它显示旧值而不是更新一个。

代码更新数据库附在下面。

@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 { } 

任何build议,欢迎。 🙂

您不调用执行更新的sqlite3_step 。 在sqlite3_preparesqlite3_prepare之前,你需要像这样的东西:

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