Macos-Fmdb无法打开数据库

时间:2019-01-21 10:44:22

标签: swift macos cocoa fmdb

当我尝试在应用程序支持目录中创建数据库文件时,出现以下错误。

  

Sqlite Mac [91210:2852867]错误打开!:14

     

打开数据库时出错:无法打开数据库文件

使用的代码如下。

func createdatabase() {
    let filemanager = FileManager.default
    let directorypath = filemanager.urls(for:.applicationSupportDirectory, in: .localDomainMask)
    let databasepath = directorypath[0].appendingPathComponent("locations.db").path
    print(databasepath)
            if !filemanager.fileExists(atPath: databasepath as String) {
        let resultdb = FMDatabase(path: databasepath as String)

        if resultdb != nil {
            if(resultdb.open()) {
                let sql_statement = "CREATE TABLE IF NOT EXISTS studentmarks (id INTEGER PRIMARY KEY AUTOINCREMENT , name Text , marks Text);"
                if !(resultdb.executeStatements(sql_statement)) {
                    print("error")
                } else
                {
                    print("Table created successfully")

                }
                resultdb.close()
            } else
            {
                print("Error while opening DB : \(resultdb.lastErrorMessage())")
            }
        } else {
           print("Error in resultdb :\(resultdb.lastErrorMessage())")
        }

    }
}

1 个答案:

答案 0 :(得分:1)

使用notify代替wait并添加完成处理程序。强制异步任务是同步的是不好的做法。

func install(lists:[String], completion: @escaping: (String)->Void) {
    let group = DispatchGroup()
    var result = ""

    if lists.isEmpty { (completion("")) }
    for i in lists {
        group.enter()
        HelperConnection.shared.getInfo(str: i, completion: { str in
            defer(group.leave())
            if str == "1" {
               result = str
            } else {
               result = "Hello"
            }
        })
    }       

    group.notify(queue: DispatchQueue.main) {
        print(".........")
        print(result)
        completion(result)
   }
}