1

I have situation :

In the newer version of the app i will add another column to the SQLite table. I need to make this change take effect when application is updating and do not lose data from database (via recreating with new structure).

May be in onUpgrade start my SQL Update scripts? How to do it correctly?

4
  • 3
    Just use Alter Table sql command and add new column. it's not effect to your data. Do it inside onUpgrade(.....) Commented Aug 2, 2016 at 8:40
  • I know sql script, but can i safely do it in the onUpgrade? Which is standard solution fot this task? Commented Aug 2, 2016 at 8:41
  • What you expect then? This is the only one and standard way. Commented Aug 2, 2016 at 8:41
  • ok, thanks for response Commented Aug 2, 2016 at 8:43

1 Answer 1

3

You have to upgrade your database version in onCreate() and then use onUpgrade(). Like this:

public YouSQLiteHandler(Context context) {
    super(context, Constants.DATABASE_NAME, null, Constants.DATABASE_VERSION); --> 1 to 2 for example


}

@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {


    addNewColumnTable();
}

When you change your database version, Android automatically call onUpgrade() and your database will be updated with all the changes that you put in onUpgrade() when the user update the app

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.