Home » Android » android – why we need to onUpgrade(); method in SQLiteOpenHelper class

android – why we need to onUpgrade(); method in SQLiteOpenHelper class

Posted by: admin June 15, 2020 Leave a comment

Questions:

I m following this tutorial.http://www.androidhive.info/2011/11/android-sqlite-database-tutorial/

can any body please make me clear this chunk of code.

 // Creating Tables
@Override
public void onCreate(SQLiteDatabase db) {
    String CREATE_CONTACTS_TABLE = "CREATE TABLE " + TABLE_CONTACTS + "("
            + KEY_ID + " INTEGER PRIMARY KEY," + KEY_NAME + " TEXT,"
            + KEY_PH_NO + " TEXT" + ")";
    db.execSQL(CREATE_CONTACTS_TABLE);
}

// Upgrading database
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
    // Drop older table if existed
    db.execSQL("DROP TABLE IF EXISTS " + TABLE_CONTACTS);

    // Create tables again
    onCreate(db);
}

Questions

What is the purpose of onUpgrade(); method?

When it is Called? as docs says this is Called when the database needs to be upgraded what does it means by upgrading the database?

Important

why we drop the table in this method and recreate?

Thanks in advance.

How to&Answers:

onUpgrade is basically for handling new db changes(could be new columns addition,table addition) for any new version of your app.

Droping the table is not always necessary in onUpgrade it all depends on what your use case is. If the requirment is to not to persists the data from your older version of app then drop should help,but if its like changing schema then it should only have alter scripts.

Answer:

Upgrade means changes have been made to the database schema(version numbers are different) and it needs to be upgraded. Dropping the table and recreating is one way to do that. You could also issue “alter table” statements.

Answer:

When you open your database it checks the version number and whether or not it exists. You can just “upgrade” your database rather than creating it new.

A good tutorial: http://www.vogella.com/articles/AndroidSQLite/article.html

Answer:

This method is called when you update your database version. It drops the existing tables and creates it again when onCreate method is called again.

Answer:

Replying to your 4 questions:

1) The purpose of onUpgrade is to manage a new database structure. You could start you app with simple features, then you need for instance to add a new column, so you need to increase the version of your database from 1 to 2 and in onUpgrade
give the instruction to add a new column, so that if the user update the app, the new column become added.

2) onUpgrade is called when you have a new version of your database and you incremented the int number in the super method( here is 1, so you eventually change it to 2)

 public static class DatabaseHelper extends SQLiteOpenHelper{
    DatabaseHelper(Context context){
        super (context,DATABASE_NAME,null,1);
    }

3) Please see above regarding what does it means to update the db

4) We Drop the table and recreate, because to modify the table (example for adding a new column that fits a new feature) a logic way to proceed could be, before to “destroy”/DROP the table and then create a new one with all the data. But this can be not the way to go although recreating the data could mean that the id numbers will be consecutive( usually are not consecutive: you could have 1, 2, and..4 because 3 has been deleted), hence dropping and then creating the table again, and eventually loading the previous data you could have this id consistency.
Sometimes you may want to use ALTER instead of DROP. Why? Usually because using DROP the user loses the content already has in the database, then if you want to learn more about Best practices and more complex real life scenarios please have a look at this amazing reply