2016-01-24 21 views
0

我知道FMdb並將其用於我的項目,但我不知道如何使用現有的數據庫文件,以及在哪裏可以將我的DB.sqlite添加到我的項目中?如何從swift項目的文件夾中使用現有的DB.sqlite?

+0

請參閱鏈接http://www.theappguruz.com/blog/use-sqlite-database-swift – kb920

+0

http://stackoverflow.com/questions/17080018/use-and-access-existing-sqlite-database-on-ios –

回答

0

你的情況:

  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.函數可以使用,只需在「func應用程序」中調用「checkDataBase()」函數即可。

相關問題