Question

I've been commissioned to update a native iOS app, but as we're also going to release it to other platforms, we're are writing the new version using Appcelerator's Titanium.

The current app uses a SQLite database to store user information. How do I access the existing SQLite database when the user updates their app to the new one? We don't want to lose any data. I assumed I would be able to access the old database and then create a migration to a new database.

And what is the best way to test this? Can I install a test app with the same app id or perhaps bundle id to simulate an update?

Thanks, Tom

Was it helpful?

Solution

This should be possible (although it can get tricky) as long as the previous developers observed Apple rules on where the user information db was stored.

As long as you have the same bundle ID, and increment the version, you can overwrite the existing app during testing to simulate an update (make sure you are using an Ad-hoc distribution), this will leave the user files in place. From there, when you load your app for the first time, load that DB in the usual way:

// Set a property so you do this only one time when they first update
if(Ti.App.Properties.getBool('isDatabaseMigrated', false)) {
    // See if the DB file exists (this may be a fresh install not an update)
    var dbfile = Ti.Filesystem.getFile(Ti.Filesystem.applicationSupportDirectory+'/database', 'nameOfDB.sql');
    if(dbfile.exists()) {
        // Open the DB
        var db = Ti.Database.open('nameOfDB');
        .... Now do your thing ....
        db.close();
    }
    // Set flag so we only check this once
    Ti.App.Properties.setBool('isDatabaseMigrated', true);
}

iOS has a specific directory where they keep DB files, and the newest version of Titanium migrates any old DB's to this directory.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top