visual-studiocordovaassetsvisual-studio-cordova

Open prepopulated SQLite database in Cordova


I am developing a cross platform (Android, IOS, WP) application that needs to have a relatively large prepopulated database. I am using Visual Studio 2013 with Cordova plugins. Most answers on the internet point here or here. However these links always start with "copy your prepopulated database to the assets folder". In my Visual Studio project, there is no assets folder. This is what I have: enter image description here

How should I go about this? Preferably the solution should work on all platforms. Thanks.


Solution

  • The SQLite forces you to create THE COMPLETE Database executing "Create Tables" commands in a function inside the App, right?

    That function ONLY executes if the Database DON'T EXIST. If already exists = don't execute that function!

    So... to the "CREATE TABLE" commands, add "INSERT INTO" commands with what you need to populate! This will only execute the first time.

    If you want a cleaner code, you can put all the data in an Array and then execute the Inserts with a For Loop.

    (I did this in many Apps and worked like a charm!)

    ADDED:

    To solve "Code Brevity" I suggest 2 Options:

    Option 1 - XML File: Add an external XML file with the info, and just read it in the "create db" function, and execute the "inserts" in a For Loop reading the XML file.

    Option 2 - create a special js file (name it "populateDB.js" for example!) and enter all the code there... you won't see the code again!

    I hope it helps because you cannot include a DB in your project, you HAVE to create it on the device...