簡體   English   中英

為什么我無法打開數據庫文件?

[英]Why do I get Unable to Open Database file?

我是 iOS 開發的初學者,我想在我的應用程序中開發一個數據庫。 但是,當我嘗試創建數據庫時,它說無法打開數據庫文件

在此處輸入圖片說明

澄清( https://github.com/stephencelis/SQLite.swift/blob/master/Documentation/Index.md

在此處輸入圖片說明

我的數據庫開發代碼如下

static func DB(){

        do {
            let db = try Connection("db.sqlite3")
            let users = Table("users")
            let id = Expression<Int64>("id")
            let name = Expression<String?>("name")
            let email = Expression<String>("email")

            try db.run(users.create { t in
                t.column(id, primaryKey: true)
                t.column(name)
                t.column(email, unique: true)
                })

        } catch {
            print("Dim background error")
        }
}

我從應用程序委托文件觸發此方法

func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
        // Override point for customization after application launch.

        AppCommon.DB()

        return true
    }

如何解決這個問題? 請幫幫我,我真的需要你的幫助..!

在iOS上,您無法在所需位置創建文件。 一個正確的位置(除其他外)在documents目錄中

引用您正在使用的庫的文檔https://github.com/stephencelis/SQLite.swift/blob/master/Documentation/Index.md#connecting-to-a-database

在iOS上,您可以在應用程序的Documents目錄中創建可寫數據庫。

 let path = NSSearchPathForDirectoriesInDomains( .DocumentDirectory, .UserDomainMask, true ).first! let db = try Connection("\\(path)/db.sqlite3") 

斯威夫特 5

文件目錄的路徑現在是一個字符串數組

let path = NSSearchPathForDirectoriesInDomains(
                .documentDirectory, .userDomainMask, true)
let db = try Connection("\(path.first ?? "")/db.sqlite3")

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM