Push incremental database updates to app - Best Practices
Hello,
I am doing some proof-of-concept work on an app that searches a local SQLite database.
My requirements:
- Application database must be updatable independently of Application updates (Database updates are more frequent that Application updates)
- Database updates are small, might occur on a weekly basis, and could be fetched as plain SQL via REST and applied by the app to the local db
- There will be no user data in the db, it is read-only for the user
Proposed process:
- Fetch update as sql string containing a dozen or so records via REST
- Backup installed database (not the one in Resources)
- Apply sql to database
- If all goes well, delete the backup
- If not, replace the currently installed db with the backup and offer to download the entire database file in its latest version
Questions:
- Do I have direct access to the installed database file via Ti.Filesystem?
- If so, what is the path to it?
- Is there a better way of doing this altogether?
Thanks
3 Answers
-
Accepted Answer
Ti.Filesystem.applicationSupportDirectory + '/database/databasename.sql'
-
Here is my code that I used, and was able to update 2 apps that have been published for a few weeks. I needed to edit 2 of my tables, and this worked flawlessly, preserving data that was already there.
var upgrade=Titanium.App.Properties.getString("upgrade"); var file = Titanium.Filesystem.getFile(Titanium.Filesystem.applicationSupportDirectory+ '/database/plotme.sqlite.sql'); if(file.exists()) { //Ti.API.info('file exists'); if (upgrade<"1.1") { var db = Titanium.Database.install('plotme.sqlite','plotme.sqlite'); var sql="ALTER TABLE locations ADD username varchar(244)"; //Ti.API.info(sql); db.execute(sql); var sql="ALTER TABLE locations ADD myhash varchar(244)"; //Ti.API.info(sql); db.execute(sql); Titanium.App.Properties.setString("upgrade","1.1"); //Ti.API.info('database upgrade'); } } //end file exist check else { //Ti.API.info('file does not exist'); var db = Titanium.Database.install('plotme.sqlite','plotme.sqlite'); Titanium.App.Properties.setString("upgrade","1.1"); }
Here's how it works
- first checks the "upgrade" status in the properties, to see what version I'm on, and to skip if needed
- it checks to see if the database is already there (because if its not an upgrade, but a fresh install, it doesnt need to do the table changes. If it doesnt exist, non of this code runs
- if it does exist, if the version is not 1.1 (the version I'm upgrading to), then run the code
- the .install runs, but since its already there, it doesnt matter
- runs my ALTER statements
- sets my "upgrade" flag
Thats it. If the file didnt exist, it would install the database that already has the changes, and sets the "upgrade" flag anyways. Now when I upgrade again, I can just add more code if needed, set the "upgrade" to whatever version, and I'm done.
One problem I did have was checking if the file exists. My database is called plotme.sqlite, but I still had to append the .sql to the end of that.
-
Now granted, mine doesnt go out and fetch, but the principle is about the same, setting flags to check the status, installing the new db, etc.