Android Question SQlite OnUpgrade

I have an application in the Android Market known as SieveSMS.It basically allows people to block incoming SMS based on rules, just like Outlook does via email.

I change the structure of the database as follows (in the same table)

1) DB Version 2

Adding a New Unread Column

2) DB version 3

Adding a New Exception Column

What happens if people who already have the application but are not upgraded to version 2 and now they see version 3 of the application? How can I make sure that changes in version 2 are also available to people?

In addition to this, what happens if people who have version 2 installed and try to upgrade to version 3. Will onUpgrade give me an error that the β€œunread” column already exists?

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

+6
source share
1 answer

There are many books on Android Development that explain onUpgrade. There, authors use the conventions to check what the old and new versions are. 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 upgrading from 1 to 3 and from 2 to 3 also works fine;)

+3
source

Source: https://habr.com/ru/post/890054/


All Articles