Open a pre-populated SQLite database in Cordova

I am developing a cross platform app (Android, IOS, WP) that needs to have a relatively large pre-populated database. I am using Visual Studio 2013 with Cordova plugins. Most of the answers on the internet point are here or here . However, these links always start with "copy your pre-populated database to assets folder". My Visual Studio project is missing assets folder. This is what I have:enter image description here

How should I do it? Preferably the solution should work on all platforms. Thank.

+3


source to share


4 answers


SQLite forces you to create a COMPLETE database executing Create Tables commands in a function within the application, right?

This function is ONLY executed if the database DOES NOT EXIST. If already exists = do not perform this function!

So ... in the "CREATE TABLE" commands add the "INSERT INTO" commands with what you need to fill! This will only run the first time.

If you want cleaner code, you can put all the data in an array and then do the inserts using a For Loop.

(I've done this in many apps and worked like a charm!)



ADDED:

To solve "Short Speech", I suggest 2 options:

Option 1 - XML ​​file: Add an external XML file with information and just read it in the "create db" function and do the "inserts" in the For loop reading the XML file.

Option 2 - create a custom js file (eg name "populateDB.js"!) And put all the code there ... you won't see the code yet!

Hope this helps, because you cannot include the DB in your project, you have to build it on the device ...

+1


source


No need to copy or paste values ​​when you first start your application.

Instead, use openDatabase with the createFromLocation parameter as described here:



http://redwanhilali.com/ionic-sqlite/

+3


source


I like your question. This got me thinking about an upcoming project and how I'm going to solve a similar problem.

The link you shared: http://gauravstomar.blogspot.com/2011/08/prepopulate-sqlite-in-phonegap.html is a good place to start. The assets directory specified is not displayed because you are viewing the Windows platform code version through Visual Studio. Note that in the Xcode / iOS screen capture in the link you link, the database file is not in the assets folder, but rather in "Resources", which roughly corresponds to the assets in the Android project.

You will need a Mac and Xcode to create your iOS project, this cannot be done with Visual Studio or on a Windows machine. When the project is created with the "cordova platform create ios" command, you will see the resource directory and you can continue with your instructions.

Likewise, with Android, you can open an Android project in Eclipse or another browser / browser file and do the same. I myself am not brave enough (lack of experience) with Visual Studio to see other platforms in a Visual Studio solution.

ADDED NOTE: At http://gauravstomar.blogspot.com/2011/08/prepopulate-sqlite-in-phonegap.html be sure to read the latest discussions / comments regarding copying the file into place in the app.Looks like it still works well but the process has changed slightly with newer versions of Cordoba.

+1


source


When using Visual Studio (and Phonegap), you cannot see the assets folder. If you want to create a pre-populated DB in Visual Studio, you are better off using the sqlite plugin.
You need two plugins.

1) cordova-sqlite-evcore-extbuild-free
2) cordova-plugin-dbcopy -> this plugin will copy your database to assets folder.

I think the following Git will help you.
https://github.com/ymochi/prepopulated-DB-for-hybrid-applications

0


source







All Articles