Where to paste sqlite DB into iOS project

132 views Asked by At

I've just been handed over an app with pretty vague instructions of how to update an internal sqlite DB.

In the past, I've done this with Cordova by just replacing an existing sqlite db into /www/ dir.

I also created a SwiftUI project using FMDB. I believe the code itself would automatically create a non-existing DB in app's Documents Directory. My code to modify the DB would then just modify this same DB in that same location. I never formally replaced the DB itself.

Anyways, instructions given to me for this app are to simply replace the DB (just a copy and paste of the .sqlite file) into the Project's root directory. This is literally just using Finder and copying and pasting. However, I don't seem to be getting the new data.

Should I be doing this in Xcode? I.e. 'Adding new file by right-clicking. Should I then copy file to project and/or add to target?'

I'm not sure this 'copy from Finder to project dir' is actually right. Quick glance at code shows that it is attempting to read DB from app's documents directory.

Is there a way to actually copy the DB to the app's documents directory?

Note: I noticed while building the app that it says something like "Copying db.sqlite to app". So that indicates to me that perhaps there's some kind of build rule that copies from the project Dir to the app's Documents Directory.

Any insights would be appreciated.

Thanks!

1

There are 1 answers

0
Shehata Gamal On

You need to add your DB to your project's structure by dragging it or File->AddFiles with selecting copy to project target

After that in your code you can read it like this

let db = Bundle.main.url(forResource: "File", withExtension: "sqlite")

And since you can't do any update operations to main bundle then you should copy this file to your document's folder the first time app launches , then read it from there any upcoming launch