gpt4 book ai didi

iphone - 在 iOS 5 上设置 sqlite 配置 SQLITE_CONFIG_SERIALIZED 返回 SQLITE_MISUSE

转载 作者:行者123 更新时间:2023-12-03 19:28:49 25 4
gpt4 key购买 nike

随着 iOS 5 的发布,我们在设置 sqlite 数据库的序列化选项时遇到越来越多的错误(因此其保存用于多线程)。我们在 sqlite3_config 上收到 SQLITE_MISUSE 错误代码。有人注意到这种奇怪的行为吗?有人知道我该如何解决这个问题吗?它在以前的 iOS 版本上运行得非常好。

这是代码:

- (sqlite3 *)getNewDBConnection {
NSLog(@"sqlite3 lib version: %s", sqlite3_libversion());

//sqlite3_config() has to be called before any sqlite3_open calls.

if (sqlite3_threadsafe() > 0) {
int retCode = sqlite3_config(SQLITE_CONFIG_SERIALIZED);
if (retCode == SQLITE_OK) {
NSLog(@"Can now use sqlite on multiple threads, using the same connection");
} else {
NSLog(@"setting sqlite thread safe mode to serialized failed!!! return code: %d", retCode);
}
} else {
NSLog(@"Your SQLite database is not compiled to be threadsafe.");
}

sqlite3 *newDBconnection;

// Open the database
if (sqlite3_open([[self getDatabaseFilePath] UTF8String], &newDBconnection) == SQLITE_OK) {
NSLog(@"Database Successfully Opened :)");
} else {
sqlite3_close(newDBconnection);
NSLog(@"Error in opening database :(");
}

return newDBconnection;
}

这是输出:

sqlite3 lib version: 3.7.7
setting sqlite thread safe mode to serialized failed!!! return code: 21
Database Successfully Opened :)

最佳答案

我也为此苦苦挣扎了很长时间,终于找到了解决方案。

正如@enobufs所说,sqlite3_config()需要在sqlite3_initialize()之前调用。但是,操作系统可能会为我们初始化 SQLite,因此我还在 sqlite3_config() 之前执行了 sqlite3_shutdown()

  1. sqlite3_shutdown()
  2. sqlite3_config()
  3. sqlite3_initialize()

然后,还需要为每个查询使用相同的连接,因为它是对序列化的数据库连接的访问​​。如此处所述 http://www.sqlite.org/capi3ref.html#sqliteconfigserialized

因此,我在应用程序启动后立即创建一个连接,并将该连接传递给每个需要它的类。

关于iphone - 在 iOS 5 上设置 sqlite 配置 SQLITE_CONFIG_SERIALIZED 返回 SQLITE_MISUSE,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7795973/

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