【问题标题】:Sqlite3 update query not working in ios appSqlite3更新查询在ios应用程序中不起作用
【发布时间】:2013-06-23 13:32:02
【问题描述】:

我正在尝试在单击按钮时更新我的​​表格列之一。

问题是代码没有给出任何异常仍然没有更新表。

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

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

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

}

欢迎提出任何建议。 :-)

【问题讨论】:

  • 这是一个很容易解决的问题。如果 sqlite 函数没有返回 SQLITE_OK 则报告从 sqlite3_errmsg() 返回的错误消息。这就是它的目的,告诉你出了什么问题。
  • 是的,从 SQLite 调用中捕获异常没有任何意义。一切都通过返回码和错误码完成。
  • 是的,正如 Hot Licks 所说; sqlite API 基于 C,而不是 Objective-C,那么它在哪里说它会抛出 Objective-C 异常(或 C++ 异常)?

标签: ios database sqlite


【解决方案1】:

您没有调用执行更新的sqlite3_step。在sqlite3_preparesqlite3_finalize 之前,您需要类似:

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-09-15
    • 2011-09-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-01-19
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多