Majkl
Majkl

Reputation: 763

Android SQLite Database update table when update app

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?

Upvotes: 1

Views: 1694

Answers (1)

Miguel Benitez
Miguel Benitez

Reputation: 2322

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

Upvotes: 3

Related Questions