gpt4 book ai didi

iphone - 我收到 sqlite3_prepare_v2 错误

转载 作者:塔克拉玛干 更新时间:2023-11-02 09:32:36 24 4
gpt4 key购买 nike

我正在创建一个应用程序,并使用 sqlite 进行更新。下面是我的一段代码:

    NSFileManager *fileMgr = [NSFileManager defaultManager];
NSString *dbPath = [[[NSBundle mainBundle] resourcePath ]stringByAppendingPathComponent:@"appforoffice.sqlite"];
BOOL success = [fileMgr fileExistsAtPath:dbPath];
if(!success)
{
NSLog(@"Cannot locate database file '%@'.", dbPath);
}
if(!(sqlite3_open([dbPath UTF8String], &database) == SQLITE_OK))
{
NSLog(@"An error has occured.");
}

const char *sql = "UPDATE settings SET `value`='Off' WHERE `type`=?";
if(sqlite3_prepare_v2(database, sql, -1, &updateStmt, NULL) == SQLITE_OK)
{
if(sqlite3_bind_text(updateStmt, 1, [updateType UTF8String], -1, SQLITE_TRANSIENT) == SQLITE_OK)
{
if(sqlite3_step(updateStmt) == SQLITE_DONE) {
NSLog(@"Update Done successfuly");
}
else {
NSLog(@"Error while updating. '%s'", sqlite3_errmsg(database));
}

sqlite3_finalize(updateStmt);
sqlite3_close(database);
}
else
{
NSLog(@"Error while binding variable. '%s'", sqlite3_errmsg(database));
}
}
else
{
NSLog(@"Error while creating update statement. '%s'", sqlite3_errmsg(database));
}

但是伙计们,我没有收到任何错误。但问题是,数据库表不受查询影响。我确信查询是完全正确的。

我对这个问题感到困惑,找不到任何解决方法。

最佳答案

sqlite3_prepare_v2() 不返回 bool 值,所以用 ! 测试它的返回值是错误的。来自reference :

On success, the sqlite3_prepare() family of routines return SQLITE_OK; otherwise an error code is returned.

所以你的代码应该看起来更像这样:

if (sqlite3_prepare_v2(database, sql, -1, &updateStmt, NULL) == SQLITE_OK)
{
if (sqlite3_bind_text(updateStmt, 1, [updateType UTF8String], -1,
SQLITE_TRANSIENT) == SQLITE_OK)
{
... call update API ...
}
else
{
NSLog(@"Error while binding variable. '%s'", sqlite3_errmsg(database));
}
}
else
{
NSLog(@"Error while creating update statement. '%s'", sqlite3_errmsg(database));
}

编辑(在使用整个查询更新问题之后):

我不喜欢查询中 ` 字符的外观;删除它们,它应该工作。

关于iphone - 我收到 sqlite3_prepare_v2 错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10310385/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com