2015-08-24 208 views
1

我想要做的是,在應用程序委託中,我想編寫一個代碼,它將複製一個sqlite數據庫,如果它不存在於iphone的文檔目錄中。爲此,我使用以下代碼 -如何將文件從目錄複製到iphone文檔目錄

func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool { 
window = UIWindow(frame: UIScreen.mainScreen().bounds) 
let containerViewController = ContainerViewController() 
window!.rootViewController = containerViewController 
window!.makeKeyAndVisible() 

//Create database if not exists 
let docsPath = NSSearchPathForDirectoriesInDomains(NSSearchPathDirectory.DocumentDirectory, NSSearchPathDomainMask.UserDomainMask, true)[0] as! String 
let databaseStr = "LocalDatabase.sqlite" 
let dbPath = docsPath.stringByAppendingPathComponent(databaseStr) 
let fileManager: NSFileManager = NSFileManager.defaultManager() 
if !fileManager.fileExistsAtPath(dbPath) { 
    let databaseInApp: String? = NSBundle.mainBundle().resourcePath?.stringByAppendingPathComponent(databaseStr) 
    fileManager.copyItemAtPath(databaseInApp!, toPath: dbPath, error: nil) 
} 

return true 
} 

它在可以在目錄中創建數據庫。但是我沒有在數據庫中獲得ant表。這意味着創建新文件而不是複製。我確信該數據庫中有9個表格是我想要複製的。文件

結構作爲screenshot-

enter image description here

如果我錯了,我不明白給出。請告訴我,如果有人能夠解決問題。還有一件事當我在模擬器中運行應用程序的時候,它運行得很好,但是在我運行它時在iphone中不起作用。

回答

3

使用此代碼:

let fileManager = NSFileManager.defaultManager() 
var error : NSError? 
var doumentDirectoryPath = NSSearchPathForDirectoriesInDomains(.DocumentDirectory, .UserDomainMask, true)[0] as! NSString 
let destinationPath = doumentDirectoryPath.stringByAppendingPathComponent("LocalDatabase1.sqlite") 
let sourcePath = NSBundle.mainBundle().pathForResource("LocalDatabase", ofType: "sqlite") 
fileManager.copyItemAtPath(sourcePath!, toPath: destinationPath, error: &error) 
+0

感謝您對代碼的幫助。當我使用'pathForResource'函數時,我使用了代碼並且在URL中得到了零。對於這個問題,我在'Copy Bundle Resources'下拖動了我的文件,它工作。 – Abhinav

2
func copyDatabase(){ 

      let fileManager = NSFileManager.defaultManager() 

     let dbPath = getDBPath() 
     var success = fileManager.fileExistsAtPath(dbPath) 


     if(!success) { 
      if let defaultDBPath = NSBundle.mainBundle().pathForResource("LocalDatabase", ofType: "sqlite"){ 

      var error:NSError? 
      success = fileManager.copyItemAtPath(defaultDBPath, toPath: dbPath, error: &error) 
      println(defaultDBPath) 
      if (!success){ 
      println("Failed to create writable database file with message\(error!.localizedDescription))") 
      } 
      }else{ 
      println("Cannot Find File In NSBundle") 
      } 
     }else{ 
      println("File Already Exist At:\(dbPath)") 
     } 
     } 


     func getDBPath()->String 
     { 

     let paths = NSSearchPathForDirectoriesInDomains(NSSearchPathDirectory.DocumentDirectory, NSSearchPathDomainMask.UserDomainMask, true) 
     let documentsDir = paths[0] as! String 
     let databasePath = documentsDir.stringByAppendingPathComponent("LocalDatabase.sqlite") 
     return databasePath; 
     } 

然後調用它didFinishLaunching

func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool { 

    copyDatabase() 
    return true 
} 
+0

感謝您的幫助。當我使用'pathForResource'函數時,我使用了代碼並且在URL中得到了零。對於這個問題,我在'Copy Bundle Resources'下拖動我的文件,它工作.. – Abhinav

1

雨燕3.0的版本

let fileManager = FileManager.default 
var doumentDirectoryPath = NSSearchPathForDirectoriesInDomains(.documentDirectory, .userDomainMask, true)[0] as NSString 
let destinationPath = doumentDirectoryPath.appendingPathComponent("LocalDatabase.sqlite") 
let sourcePath = Bundle.main.path(forResource: "LocalDatabase", ofType: "sqlite") 
do{ 
    try fileManager.copyItem(atPath: sourcePath!, toPath: destinationPath) 
}catch let error as NSError { 
    print("error occurred, here are the details:\n \(error)") 
} 
相關問題