4

I'm trying to upload my sqlite database file into an application. I've been learning about the iOS file system, and I'm not completely sure how it works and got lost. This is what I would like to achieve, but not sure how:

  1. I would like to have database on this location my-xcode-project-path/data/foo.sqlite. Now I'm running simulator for first time, and I would like to copy this database into simulator's Document directory.
  2. In case if I had the application already installed, I would skip step 1. and copy database from bundle.
  3. If I tried to run simulator, but don't have file available in bundle, I would like to keep that database.

Thank you in advance!!!

My code is looking like this:

func prepareDatabaseFile() -> String {
    let fileName: String = "foo.sqlite"

    let filemanager:FileManager = FileManager.default
    let directory = filemanager.urls(for: .documentDirectory, in: .userDomainMask).first!

    let newUrl = directory.appendingPathComponent(fileName)
    let bundleUrl = Bundle.main.resourceURL?.appendingPathComponent(fileName)

    // check bundle
    if filemanager.fileExists(atPath: (bundleUrl?.path)!) {
        print("bundle file exists!")
        return (bundleUrl?.path)! //probably I need to copy from bundle to new app and return new url
    // here check if file already exists on simulator, but not in bundle
    } else if filemanager.fileExists(atPath: (newUrl.path)) {
        print("prebuild file exists!")
        return newUrl.path //no copy is needed
    // finally, if nothing I need to copy local file
    } else {
        //todo copy local file from the path my-xcode-project-path/data/foo.sqlite
        print("todo")
    }

    return fileName
}
9
  • The typical process is "try to open db in Documents and if not there, copy file from bundle" or, anticipating v2 process "check version in documents and if not found, copy from bundle; but if db is found in Documents, but version is prior to what is included in this version of the tool, upgrade the version in documents (or replace w bundle version)". I don't understand your scenario 3 in you question, though. In what scenario do you anticipate not having db in the bundle? How is that possible? Commented Jun 5, 2017 at 20:11
  • HI Rob, thanks a lot for your quick reply. Scenario 3 is when I'm copying database from project directory. During development, especially on very beginning I would force copying fresh database from my local computer very often, dropping everything what I've deployed so far. I'm missing the way how such files are moved from local project into simulator's Document directory. Commented Jun 5, 2017 at 20:23
  • Personally, when I want to blow away what's in Documents folder, I just uninstall the app. When I install it again, Documents folder is blank and it falls into the first scenario. Or, I'd tweak the version number in the database in the project (that gets included in the bundle automatically), and rely on the "if prior version, replace with bundle copy" logic. Commented Jun 5, 2017 at 20:29
  • By the way, rather than "see if the file is in documents" logic, I'd just open the Documents database (using sqlite3_open_v2 with "readwrite" option, but not "create" option), and only if it failed (or the version was old) would I copy from bundle and then open it again. It is more efficient, because generally when the user fires up the app, the db is there already; it's only the first time (or every upgrade) that you might need to copy. To get more specific, we'd need to know how you're interacting with SQLite. I.e. some library or are you doing sqlite3_xxx calls yourself? Commented Jun 5, 2017 at 20:33
  • Thanks Rob!!! You said that your database gets included in the bundle automatically, I think that I'm missing that part. Could you explain me, how to include database file into the bundle? Commented Jun 5, 2017 at 20:57

2 Answers 2

5

With @Rob's help, I came to following solution which satisfied my requirement. Previously it was necessary to add sqlite file into xcode project, with proper target.

func prepareDatabaseFile() -> String {
    let fileName: String = "foo.sqlite"

    let fileManager:FileManager = FileManager.default
    let directory = fileManager.urls(for: .documentDirectory, in: .userDomainMask).first!

    let documentUrl= directory.appendingPathComponent(fileName)
    let bundleUrl = Bundle.main.resourceURL?.appendingPathComponent(fileName)

    // here check if file already exists on simulator
    if fileManager.fileExists(atPath: (documentUrl.path)) {
        print("document file exists!")
        return documentUrl.path
    else if fileManager.fileExists(atPath: (bundleUrl?.path)!) {
        print("document file does not exist, copy from bundle!")
        fileManager.copyItem(at:bundleUrl, to:documentUrl)
    }

    return documentUrl.path
}
Sign up to request clarification or add additional context in comments.

Comments

0
  self.copyfile(filename: "mydata.sqlite" )

        return true
    }

    func copyfile(filename : String)
    {
        let dbpath : String = getpath(filename: filename as String)

        let filemanager = FileManager.default

        if filemanager.fileExists(atPath: dbpath)
        {
            let documentsURl = Bundle.main.resourceURL

            let frompath = documentsURl?.appendingPathComponent(filename)

            print(frompath)

        }



    }

    func getpath(filename: String) -> String
    {

        let documentsURL = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask)[0]

        print(documentsURL)

        let fileURL = documentsURL.appendingPathComponent(filename as String)

        print(fileURL.path)
        return fileURL.path




    }

1 Comment

Code only answers arent encouraged as they dont provide much information for future readers please provide some explanation to what you have written

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.