Database Not Updating with App Update
It appears that when you install a database in your app, it puts it in the user's data folder in the iOS. Database changes don't get pulled into the app without deleting and reinstalling the app (an app update leaves the user's data intact).
What's the best way to make sure your app pulls in the new database on subsequent app updates?
11 Answers
-
I realized that since your users will update at random intervals (some skip 1.1 and upgrade when they see 1.2 or something) then you don't want to keep your version number in the database name (airports1, airports2, etc.). But it works just as well to just use the same DB name every time. The cached file still gets removed and reinstalled. This also means you don't need a one-time-only condition like I do. So the final code would look like this:
//Reference prior, cached database var dbOLD = Titanium.Database.install('airports.db', 'airports'); //Destroy it dbOLD.remove(); //Install new database var db = Titanium.Database.install('airports.db', 'airports');
So even first-time users will install the DB twice, but it's just to make sure a cached version isn't in there.
I find that I have to build my app a couple times after a DB change for it to start showing up, so don't worry if your first build doesn't seem to have the DB changes.
-
Okay, I think I've got it. Let me know what you think of this:
//Reference prior, cached database var dbOLD = Titanium.Database.install('stuff10.db', 'stuff'); //Destroy it dbOLD.remove(); //Install new database var db = Titanium.Database.install('stuff11.db', 'stuff');
Thoughts?
-
Okay, I think I've finally got this figured out.
When you use the
Database.install
function, it copies the database from your Resources folder into the app's cache (~/Library/Application Support/iPhone Simulator/4.x/Applications/1234ABCetc/Library/Application Support/database
).Hang on while I catch my breath after typing that path. Okay.
It copies it there and names it whatever is in the 2nd attribute of the install function:
var db = Titanium.Database.install('airports.db', 'airports123');
So the cached version of the DB will be
airports123.sql
.So here's how you remove the cached version and update to the latest database. Keep in mind that you never rename your core DB that's in the Resources folder. That's the primary data source.
//Put everything in an if statement controlled by a flag you so you only execute this once per app build var firstTime = Titanium.App.Properties.getString('firstTime'); if(firstTime != 'no'){ Titanium.App.Properties.setString('firstTime','no'); //Reference prior, cached database var dbOLD = Titanium.Database.install('airports.db', 'airports1'); //Destroy it dbOLD.remove(); //Install new database var db = Titanium.Database.install('airports.db', 'airports2'); }
This will make a new copy of your DB with the
airports2
name and your users will have fresh data when they update to this version.Good luck!
-
I've yet to deal with this in my app. But I need to before I publish. From what I've seen in other postings is that you have to handle this within your app. For example, you might create a db table with a single record containing your app's database schema version number. When the app launches, it can check that version and if it should be newer, then your app would go through an upgrade routine. This gives you the flexibility to update the code without going through a database change (if your updates didn't involve any DB changes).
Tim
-
-subscribing-
-
I've been getting errors and crashes trying to use the db.remove() technique. Unfortunately I have yet to find a consistent, reliable way to update the database with an app update. Even renaming the database hasn't worked for me yet.
-
I know this thread is old, but found a great solution here:
http://labrum.co/articles/titanium/using-a-local-database-with-titanium/comment-page-1/#comment-2543
-
Hi,
I use this function for get the path to sql file generate by Ti.Database.install
// sqlFile is the name of database // example : // If your code like this - > Ti.Database.install('iphone.sqlite', 'main'); // you write this - > getSqlFilePath('main'); function getSqlFilePath(sqlFile) { sqlFile = sqlFile + '.sql'; var path = Ti.Filesystem.applicationDataDirectory; path = path.replace('Documents', 'Library'); path = path + 'Private%20Documents/'+sqlFile; return path; }
and for delete this, you must just write this ->
var sqlFilePath = getSqlFilePath('main'); var oldBddSql = Ti.Filesystem.getFile(sqlFilePath); oldBddSql.deleteFile();
This code work fine only on iOS < 6.1.4
On 6.1.4, the sql file exist too but deleteFile() has no effect.my code :
oldBddSql.deleteFile(); Ti.API.info(oldBddSql.exists()); // return true...
maybe sql file is protected ? have you an idea?
Tanks.
-
-subscribing-
-
-subscribing-
-
One possibility is to give your database a new name with each release, but I imagine this will build up a pile of unused databases in the user's data folder.
var db = Titanium.Database.install('stuff10.db', 'stuff');
var db = Titanium.Database.install('stuff11.db', 'stuff');
var db = Titanium.Database.install('stuff12.db', 'stuff');
etc.
Seems so dirty…