Comments on: Upgrade Android SQLite Database http://blog.adamsbros.org/2012/02/28/upgrade-android-sqlite-database/ Thu, 20 Apr 2017 16:07:44 +0000 hourly 1 https://wordpress.org/?v=5.2.2 By: Angie http://blog.adamsbros.org/2012/02/28/upgrade-android-sqlite-database/comment-page-1/#comment-84645 Thu, 20 Apr 2017 16:07:44 +0000 http://blog.adamsbros.org/?p=431#comment-84645 No, it’s easy. The number of progress ticks is newVersion – oldVersion and each case can update the progress by one.

]]>
By: Paritosh http://blog.adamsbros.org/2012/02/28/upgrade-android-sqlite-database/comment-page-1/#comment-56688 Sat, 14 Nov 2015 22:37:23 +0000 http://blog.adamsbros.org/?p=431#comment-56688 Been trying a couple of approaches but this one does the job efficiently for apps with relatively shorter database version changes.

]]>
By: Paul http://blog.adamsbros.org/2012/02/28/upgrade-android-sqlite-database/comment-page-1/#comment-43022 Mon, 08 Jun 2015 06:11:31 +0000 http://blog.adamsbros.org/?p=431#comment-43022 I’ve used this pattern for years.
Note that when the # of upgrades gets very large (I was up to 300+), the switch will fail because it can overtax the compiler.

Instead, simply have a function for each upgrade step, ie
current_version = upgrade_step_10( current_version );

And one master function that calls ALL of those functions in order.
The trick is that each function checks the current_version parameter to see if this step should be performed. If not, then it just returns current_version.

Something like that.

You can put all the functions in a nice big array of function pointers (yes it was in C++) and for loop through them.

An additional trick is to ensure that after each step, you also write back to the DB what the new version number is.

That way if the upgrade breaks half way through, it is possible to continue the upgrade later (although hand-fixing may be required if a step breaks halfway through).

]]>
By: Hamza http://blog.adamsbros.org/2012/02/28/upgrade-android-sqlite-database/comment-page-1/#comment-30714 Tue, 17 Feb 2015 16:37:49 +0000 http://blog.adamsbros.org/?p=431#comment-30714 Thanks a lot Adams.

]]>
By: Kaitlyn http://blog.adamsbros.org/2012/02/28/upgrade-android-sqlite-database/comment-page-1/#comment-27715 Mon, 19 Jan 2015 21:40:39 +0000 http://blog.adamsbros.org/?p=431#comment-27715 Got someone on stackoverflow to confirm what I thought. Thanks again for the template for onUpgrade!

]]>
By: Kaitlyn http://blog.adamsbros.org/2012/02/28/upgrade-android-sqlite-database/comment-page-1/#comment-26471 Sat, 10 Jan 2015 16:26:09 +0000 http://blog.adamsbros.org/?p=431#comment-26471 Thanks for this — it’s a perfect template for onUpgrade.

I’m having to deal with changing my schema for the first time.

Do I just:
1) Change this var: public static final int DB_VERSION = 1; which is fed into SQLiteOpenHelper’s constructor
2) Add some code in onUpgrade() like you have here

Do I also change my onCreate() to reflect the new schema OR do I leave it as is (the version 1 schema)?

Thanks for any help!

]]>
By: zili http://blog.adamsbros.org/2012/02/28/upgrade-android-sqlite-database/comment-page-1/#comment-25709 Tue, 06 Jan 2015 11:19:02 +0000 http://blog.adamsbros.org/?p=431#comment-25709 it can remove the while loop and all the breaks in the switch, then the upgrade can also work and looks simpler.

]]>
By: Trenton http://blog.adamsbros.org/2012/02/28/upgrade-android-sqlite-database/comment-page-1/#comment-18211 Thu, 27 Nov 2014 08:10:27 +0000 http://blog.adamsbros.org/?p=431#comment-18211 1. Switch/case without breaks is usually considered bad practise, as it can result in bugs.
2. If you had a large number of database versions to upgrade through, you might want to display a progress bar. Doing it via a loop would make that easier.

]]>
By: Jessica http://blog.adamsbros.org/2012/02/28/upgrade-android-sqlite-database/comment-page-1/#comment-17980 Wed, 26 Nov 2014 11:07:25 +0000 http://blog.adamsbros.org/?p=431#comment-17980 I am wondering if this works as well

public void onUpgrade(
final SQLiteDatabase db, final int oldVersion,
final int newVersion)
{
int upgradeTo = oldVersion + 1;
switch (upgradeTo)
{
case 5:
db.execSQL(SQLiteSet.V5_ADD_LAST_CARD);
db.execSQL(SQLiteCard.V5_ADD_FAILED);
case 6:
db.execSQL(SQLiteSet.V6_ADD_IMPORT_TYPE);
case 7:
db.execSQL(SQLiteSet.V7_ADD_SHORT_FNAME);
break;
}
}

So if upgradeTo is 6, it will do both case 6 & 7

]]>
By: Trenton http://blog.adamsbros.org/2012/02/28/upgrade-android-sqlite-database/comment-page-1/#comment-14778 Fri, 07 Nov 2014 04:28:57 +0000 http://blog.adamsbros.org/?p=431#comment-14778 I apologize, I did not see your question earlier. Yes, those are my own custom classes, which implement sets of cards for a flash card program for android. Not really relevant to this specific blog post though, which is why I didn’t include them.

]]>