【问题标题】:How do I seed a database in Mac OS X application如何在 Mac OS X 应用程序中为数据库播种
【发布时间】:2016-01-21 17:31:58
【问题描述】:

我想将存储在应用程序包中的 sqlite 数据库添加到 Mac OS X 中的应用程序数据存储文件路径中,我该如何使用 Apple 新的 Swift 编程语言来做到这一点?

【问题讨论】:

    标签: database swift sqlite seed


    【解决方案1】:

    我能够弄清楚这一点,所以我想我会发布它以供其他人使用。

    从应用程序包中使用 Swift 将种子 DB 用于 Mac OS X 应用程序:

    创建一个函数来处理检查数据库是否已经存在,然后从 NSPersistentStoreCoordinator 调用 if,“DBName”是您的数据库的名称:

    func openDB()
    {
    
        let storePath = applicationDocumentsDirectory.URLByAppendingPathComponent(“DBName.sqlite").path
        let dataPath = NSBundle.mainBundle().pathForResource(“DBName", ofType: "sqlite")
        //let testPath = storePath.path
        //let testPath2 = storePath
        if !NSFileManager.defaultManager().fileExistsAtPath(storePath!)
        {
            do
            {
                try NSFileManager.defaultManager().copyItemAtPath(dataPath!, toPath: storePath!)
            } // end do
            catch
            {
                NSLog("Error copying database file")
            } // end catch
        } // end if
    } // end openDB
    

    我在NSPersistentStoreCoordinator 中调用它,所以它只被调用一次

    lazy var persistentStoreCoordinator: NSPersistentStoreCoordinator = {
    

    应用程序的持久存储协调器。此实现创建并返回一个协调器,并将应用程序的存储添加到它。此属性是可选的,因为存在可能导致商店创建失败的合法错误条件。

        // Create the coordinator and store
        self.openDB()
        let coordinator = NSPersistentStoreCoordinator(managedObjectModel: self.managedObjectModel)
        let storeURL = self.applicationDocumentsDirectory.URLByAppendingPathComponent(storeFilename)
        var failureReason = "There was an error creating or loading the application's saved data."
        do
        {
            //try coordinator.addPersistentStoreWithType(NSSQLiteStoreType, configuration: nil, URL: storeURL, options: nil)
            try coordinator.addPersistentStoreWithType(NSSQLiteStoreType, configuration: nil, URL: storeURL, options: [NSMigratePersistentStoresAutomaticallyOption: true,
                NSInferMappingModelAutomaticallyOption: true])
        }
        catch
        {
            // Report any error we got.
            var dict = [String: AnyObject]()
            dict[NSLocalizedDescriptionKey] = "Failed to initialize the application's saved data"
            dict[NSLocalizedFailureReasonErrorKey] = failureReason
    
            dict[NSUnderlyingErrorKey] = error as NSError
            let wrappedError = NSError(domain: domainName, code: 9999, userInfo: dict)
            // Replace this with code to handle the error appropriately.
            // abort() causes the application to generate a crash log and terminate. You should not use this function in a shipping application, although it may be useful during development.
            NSLog("Unresolved error \(wrappedError), \(wrappedError.userInfo)")
            abort()
        }
    
        return coordinator
        }()
    

    【讨论】:

    • 哇,你很快就找到了问题的答案!
    猜你喜欢
    • 1970-01-01
    • 2015-04-23
    • 1970-01-01
    • 1970-01-01
    • 2012-04-18
    • 2012-07-28
    • 1970-01-01
    • 2013-03-30
    • 2012-06-01
    相关资源
    最近更新 更多