This is my second "Guest Blog Post" for the month. Sorry I've been a bit slow on my own content lately! Today's post comes from Scott Buckel. He and I shared a conversation earlier this month about databases and PhoneGap. PhoneGap makes it pretty darn easy to create and work with a database in your application. What isn't so trivial is actually shipping a prepopulated database with the app itself. I wasn't able to help him as much as I'd like - but he worked at it until he came up with a solution. Here is what he discovered.
These instructions are very raw, and not optimized.
- I used this plugin: https://github.com/chbrody/Cordova-SQLitePlugin/
- Copy your sqlite db file to /assets folder of PhoneGap.
- Then, follow instructions here: http://gauravstomar.blogspot.com/2011/08/prepopulate-sqlite-in-phonegap.html to copy the file to native storage. Change this.copy("Databases.db","/data/data/"+pName+"/app_database/");
- Instead of Databases.db, use your database filename.
- Instead of app_database, use "databases"
- You'll probably want to delete the file from /assets, since it is duplicated and no longer needed. My app was double the size it needed to be.
- (Not sure if this step is necessary, I'm getting out of the office for the day). Edit SQLitePlugin.java
- Lines 176, I edited 1==1 so that if statement is always executed. Line 179 I changed this.setStorage(appPackage, false); to this.setStorage(appPackage, true);
- You can then use the following command to open the DB and use it as any other PhoneGap database
- var db = window.sqlitePlugin.openDatabase("[full_database_name_including_extension]", "1.0", "PhoneGap Demo", 200000);
10 hours of work later, I have a working database!
Feel free to use this information on a blog, I feel it would help a LOT of people out. Step #4 is weird, I'm sure there's a "prettier" way to do it. If used, just give Scott Buckel @ Corporate Zen credit please :).