vendredi 3 juillet 2015

Why create an empty database (0 bite)?

My application used sqllite Databases and I need to copy Datebase in the Documents Folders . But when checking if there is already a database file , it is already there , and does not copy. At the start of the application in a documents folder, create an empty file.

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)

Aucun commentaire:

Enregistrer un commentaire