-2
我有以下sqllite代碼:SQLLite錯誤 - 無法打開DB
func createAndCheckDatabase()-> Bool
{
var success: Bool = true
var db:COpaquePointer = nil // Get path to DB in Documents directory
let docDir:AnyObject = NSSearchPathForDirectoriesInDomains(.DocumentDirectory, .UserDomainMask, true)[0]
let path = docDir.stringByAppendingPathComponent("MyDatabase.db")
// Check if copy of DB is there in Documents directory
let fm = NSFileManager.defaultManager()
if !(fm.fileExistsAtPath(path)) {
// The database does not exist, so copy to Documents directory
let from = NSBundle.mainBundle().resourcePath!.stringByAppendingPathComponent(databaseName)
var error:NSError?
if !fm.copyItemAtPath(from, toPath: path, error: &error) {
//ALWAYS ERRORS HERE THE FIRST TIME
println("SQLiteDB - #1 failed to open DB.")
println("Error - \(error!.localizedDescription)")
}
}
databasePath = path
// Open the DB
let cpath = (path as NSString).UTF8String
let error = sqlite3_open(cpath, &db)
if error != SQLITE_OK {
// Open failed, close DB and fail
println("SQLiteDB - another error - couldn't open DB")
sqlite3_close(db)
}
return success
}
我打電話給我的應用程序委託中此功能與思想,它會成功創建我的數據庫一次(也是唯一一次)。每當我清除設置並運行它時,它總是會打到我標記過的區域(錯誤)一次。再次運行後,我再也不會遇到這個錯誤。
這段代碼是否存在一些邏輯瑕疵(我主要複製了這段代碼)還是我可能報告了一個實際上不是的錯誤?我懷疑它可能只會在第一次創建時發生,但我確實沒問題,並且可以開始與數據庫進行交互。
也有人看到有關代碼中的東西?
謝謝!
這是不是一個實際的答案。如果您實際上發佈了代碼修補而不是鏈接,那將會更有幫助。 – rmaddy 2014-09-22 01:52:31
好的,謝謝我加了它。 – NullHypothesis 2014-09-22 02:29:14