如何在swift项目中使用文件夹中的现有DB.sqlite?

时间:2016-01-24 10:06:06

标签: ios swift2 fmdb

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

1 个答案:

答案 0 :(得分:0)

在你的情况下:

  1. drop" 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. 3.Function即可使用,只需调用" checkDataBase()"函数" func application"也许..