問題描述
隨著 iOS 5 的發(fā)布,我們?cè)跒?sqlite 數(shù)據(jù)庫設(shè)置序列化選項(xiàng)時(shí)遇到越來越多的錯(cuò)誤(因此它的保存用于多線程).我們?cè)?sqlite3_config 上收到 SQLITE_MISUSE 錯(cuò)誤代碼.有人注意到這種奇怪的行為嗎?有人知道我該如何解決這個(gè)問題嗎?它在以前的 iOS 版本上運(yùn)行良好.
With the release of iOS 5 we are getting more and more errors when setting the serialized option for the sqlite database (so its save to be used for multithreading). We are getting SQLITE_MISUSE error code on sqlite3_config. Has someone noticed this odd behavior? And does someone know how I can fix this? It works perfectly fine on previous iOS versions.
代碼如下:
- (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 :)
推薦答案
我也為此苦苦掙扎,終于找到了解決方案.
I struggled long and hard with this as well and finally got the solution.
正如@enobufs 所說,sqlite3_config()
需要在sqlite3_initialize()
之前調(diào)用.但是,操作系統(tǒng)可能會(huì)為我們初始化 SQLite,所以我還在 sqlite3_config()
之前做了一個(gè) sqlite3_shutdown()
.
As @enobufs said, sqlite3_config()
needs to be called before sqlite3_initialize()
. However, the OS might initialize SQLite for us so I also do a sqlite3_shutdown()
before the sqlite3_config()
.
sqlite3_shutdown()
sqlite3_config()
sqlite3_initialize()
.
然后它還需要為每個(gè)查詢使用相同的連接,因?yàn)樗菍?duì)被序列化的數(shù)據(jù)庫連接的訪問??.如此處所述 http://www.sqlite.org/capi3ref.html#sqliteconfigserialized
Then its also necessary to use the same connection for every query as it is the access to the database connection that gets serialized. As described here http://www.sqlite.org/capi3ref.html#sqliteconfigserialized
因此,我會(huì)在應(yīng)用啟動(dòng)后立即創(chuàng)建一個(gè)連接,并將該連接傳遞給每個(gè)需要它的類.
So I create a connection as soon as the app starts up and the pass that connection to every class that needs it.
這篇關(guān)于在 iOS 5 上設(shè)置 sqlite config SQLITE_CONFIG_SERIALIZED 返回 SQLITE_MISUSE的文章就介紹到這了,希望我們推薦的答案對(duì)大家有所幫助,也希望大家多多支持html5模板網(wǎng)!