带有实际设备的SqlLite无法正常运行,但可以在模拟器上正常运行

时间:2019-01-02 18:13:18

标签: xcode sqlite swift4.2

我正在使用sqlLite with this library,所以当我在模拟器上运行时,它运行良好,但是当我尝试运行到真实设备上时,却出现了此错误

错误:

  

/ var / mobile /容器/数据/应用程序/ B8B355F9-6378-4862-9D06-84DA3FD06366 /文档
  2019-01-02 19:54:23.476422 + 0200 Test [3626:1280621] [logging-persist]无法在[95fbac39ba]的42249行打开文件
  2019-01-02 19:54:23.476491 + 0200 Test [3626:1280621] [记录持久性] os_unix.c:42249:(0)打开(/ var / mobile / Containers / Data / Application / B8B355F9-6378-4862 -9D06-84DA3FD06366 / DocumentsTest.sqlite3)-未定义错误:0   无法连接到数据库,错误是:Error Domain = SQLite.Result Code = 0“(null)”,[:]

和我与dp建立连接的这段代码:

    import Foundation
    import SQLite
    import SQLite3

    class Database{

    static let shared = Database()
    public let connection: Connection?
    public let databaseName = "Test.sqlite3"

    init() {
        let dbPath = NSSearchPathForDirectoriesInDomains(.documentDirectory, .userDomainMask, true).first as! String
        print(dbPath)
        do{
            connection = try Connection("\(dbPath)\(databaseName)")
        }catch{
            connection = nil
            let nserror = error as NSError
            print("Can't connect to DB, Error is: \(nserror), \(nserror.userInfo)")
        }
    }
}

1 个答案:

答案 0 :(得分:0)

您需要添加一个“ /”

 connection = try Connection("\(dbPath)/\(databaseName)")