Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android SQlite OnUpgrade question

Tags:

android

sqlite

I have an app in the Android Market known as SieveSMS.It basically lets people block incoming SMS's based on rules, just like outlook does to emails.

I have changes the the DB structure as follows (in the same table)

1)DB Version 2

Addition of a new column "unread"

2)DB version 3

Addition of a new column "exception"

What would happen if people who already have the app but have not upgraded to version 2, and they now see a version 3 of the app? How do I make sure that changes in version 2 also get available to people?

In addition to that, what would happen if people who have version 2 installed and they try to upgrade to version 3.Will the onUpgrade give me an error that the column "unread" already exists?

Do you think it would be wise to have code conditional in onUpgrade method based on the current version of the DB?

like image 593
Rupin Avatar asked Jun 08 '11 12:06

Rupin


1 Answers

There are many books about Android Development that explain the onUpgrade. There the authors use conditionals to check what the old and new version is. The solution to your problem is very simple:

if (oldVersion < 2) {
  db.execSQL(db, SQL_ADD_UNREAD);                                                                          
}
if (oldVersion < 3) {
  db.execSQL(db, SQL_ADD_EXCEPTION);                                                                        
}

Now there is no problem when upgrading from 1 to 3 and 2 to 3 also works fine ;)

like image 139
dst Avatar answered Oct 21 '22 04:10

dst