开发者

Android SQlite OnUpgrade question

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 wh开发者_JAVA技巧o 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?


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 ;)

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜