【问题标题】:How can i use existing DB.sqlite from folder in swift project?我如何在 swift 项目的文件夹中使用现有的 DB.sqlite?
【发布时间】:2016-04-30 16:35:39
【问题描述】:

我知道FMdb 并将其用于我的项目,但我不知道如何使用现有的数据库文件以及在哪里可以在我的项目中添加我的DB.sqlite

【问题讨论】:

标签: ios swift2 fmdb


【解决方案1】:

在你的情况下:

  1. 将“DB.sqlite”拖放到您的项目文件夹中(在导航窗口中)
  2. 将以下代码添加到您的 AppDelegate.swift 文件中

        func checkDataBase(){
    print("check database...")//log
    let url = self.applicationDocumentsDirectory.URLByAppendingPathComponent("DB.sqlite")
    // Load the existing database
    if !NSFileManager.defaultManager().fileExistsAtPath(url.path!) {
        print("Not found, copy one!!!")//log         
        let sourceSqliteURL = NSBundle.mainBundle().URLForResource("DB", withExtension: "sqlite")!
        let destSqliteURL = self.applicationDocumentsDirectory.URLByAppendingPathComponent("DB.sqlite")        
        do {
            try NSFileManager.defaultManager().copyItemAtURL(sourceSqliteURL, toURL: destSqliteURL)
        } catch {
            print(error)
        }           
    }else{
    print("DB file exist")//log
    }}
    

3.Function 已经可以使用了,可以在“func application”中调用“checkDataBase()”函数..

【讨论】:

    猜你喜欢
    • 2012-05-29
    • 2019-06-14
    • 2015-07-29
    • 1970-01-01
    • 1970-01-01
    • 2017-10-29
    • 1970-01-01
    • 1970-01-01
    • 2011-12-17
    相关资源
    最近更新 更多