为什么要创建一个空数据库(0咬)?

时间:2015-07-03 11:43:10

标签: ios database swift sqlite xcode6

我的应用程序使用了sqllite数据库,我需要在文档文件夹中复制Datebase。但是在检查是否已有数据库文件时,它已经存在,并且不会复制。在文档文件夹中的应用程序开始时,创建一个空文件。

import Foundation
import UIKit
import SQLite

public class myUITabBarController: UITabBarController, NSURLConnectionDelegate, NSXMLParserDelegate {

  var db = Database("\(NSSearchPathForDirectoriesInDomains(.DocumentDirectory, .UserDomainMask, true).first as! String)/Base.db")

.......

func pathToDocsFolder() -> String {
    let pathToDocumentsFolder = NSSearchPathForDirectoriesInDomains(.DocumentDirectory, .UserDomainMask, true)[0] as! String
    return pathToDocumentsFolder.stringByAppendingPathComponent("/Base.db")
}


 public override func viewDidLoad() {
    super.viewDidLoad()



    let theFileManager = NSFileManager.defaultManager()

    if theFileManager.fileExistsAtPath(pathToDocsFolder()) {

        println("File Found!")
        // And then open the DB File
    }
    else {
        // Copy the file from the Bundle and write it to the Device:
        let pathToBundledDB = NSBundle.mainBundle().pathForResource("Base", ofType: "db")
        let pathToDevice = pathToDocsFolder()

        let error:NSError?

        // Here is where I get the error:
        if (theFileManager.copyItemAtPath(pathToBundledDB!, toPath:pathToDevice, error:nil)) {
            // success
        }
        else {
            // failure 
        }
    }


    var mPath = pathToDocsFolder()
     db = Database(mPath)

1 个答案:

答案 0 :(得分:0)

试试这个:从包中复制基础