How can i pre-populate SQLite DB for an app?


#1

Hello. I’m creating an app with SQLite DB.
I need to pre-populate the db: I have a json file with my data, but I don’t know how can I seed the DB just the first time I open my app.

Any idea?

Or maybe is there a way to open a sqlite db file?


#2

In such cases I find it a good idea to have a config table with name and value fields.

When your app starts, after db init, check for the config with your desired name and check what it’s value is.
If it’s not what you expect, initiate the json file parsing, insert the data to the db and finally update your config tables name config and change the value to what it should be when the json file has been imported.

So next time your app starts and checks the config and sees that the file has been imported, skips it.


#3

So:

  1. My json file in a data folder
  2. The first time I open the app on Ionic Ready I check for the field “imported” in ‘question_setting’ table in my DB.
  3. If imported is false I load my json and i loop inside the json to create my data. I show progress bar. In imported is true I skip.
  4. I check for an update_timestamp.json: if present I update the question from the json file (so I can push updates) and I add the name of the revision in the latest_rev in ‘question_setting’ table.

Sound good? I don’t know if I can produce an app like this but I will try! :smile: