Ganesh Ramachandran
Ganesh Ramachandran

Reputation: 181

How to make changes to the database without having to uninstall APK every time?

public class ProjectConstants {
    public static final String myDbName = "ContactsDatabase.db";
    public static final String myTableName = "ContactDetails";
   // public static final String myTableName = "ContactDetails1";
    public static final String nameColumn = "ContactName";
    public static final String numberColumn = "ContactNumber";
}

First I create a contact application with Table Name : ContactDetails Then I do all the adding, editing and deleting operations over that database table. Say I have 15 contact details in that database table named ContactDetails.

Now in the above code snippet I make 2 changes: I comment this line

//public static final String myTableName = "ContactDetails";

And I uncomment this line

public static final String myTableName = "ContactDetails1";

Now if I run the code, the App crashes.

My learning from Stack Overflow has crudely taught me 2 things to ponder

1.

Remember to uninstall and then reinstall your app so the onCreate method is called again and the database recreated.

2.

If the database is created for the first time, the onCreate method is called in the helper class, otherwise onUpgrade is called.

My subclass that extends SQLiteOpenHelper class looks like this,

public class DBHelper extends SQLiteOpenHelper {

    private static final String TEXT_TYPE = " TEXT";
    private static final String COMMA_SEP = ",";
    private static final String SQLCreateEntries =
            "CREATE TABLE " + ProjectConstants.myTableName + " (" +
                    ProjectConstants.nameColumn + TEXT_TYPE + COMMA_SEP +
                    ProjectConstants.numberColumn + TEXT_TYPE + " )";
    private static final String SQLDeleteEntries = "DROP TABLE IF EXISTS " + ProjectConstants.myTableName;

    public DBHelper(Context context) {
        super(context, String.valueOf(name), null, version);
    }


    @Override
    public void onCreate(SQLiteDatabase sqLiteDatabase) {
        sqLiteDatabase.execSQL(SQLCreateEntries);
    }

    @Override
    public void onUpgrade(SQLiteDatabase sqLiteDatabase, int i, int i1) {
        sqLiteDatabase.execSQL(SQLDeleteEntries);
        onCreate(sqLiteDatabase);
    }

Now if I follow the instruction in the 1st quote block by uninstalling and reinstalling the application every time I mess with the database, the app never crashes. But if I revert back to the original database,(then I do this uninstalling and reinstalling the application) obviously my data in the database do not exist anymore.

As mentioned above my Database table named "ContactDetails" which held 15 contacts will no more have that data now.

But I want that 15 contacts to stay intact inside my database Table "ContactDetails".

Is there anyway to make changes to the database without having to uninstalling-reinstalling apk and without losing the previous data ?

Upvotes: 1

Views: 69

Answers (1)

Naitik
Naitik

Reputation: 816

You have write SQL Commands and execute block of SQL commands with different version of code on OnUpgrade() of Helper class as follows:

@Override
    public void onUpgrade(SQLiteDatabase sqLiteDatabase, int i, int i1) {
        sqLiteDatabase.execSQL(SQLDeleteEntries);
        onCreate(sqLiteDatabase);
        switch(i){
           case 1:
               <commands for version 1>
               break;
           case 2:
                <commands for version 2>
                break;
    }

You can write the swich cases as per version of applications. you can write different block of SQL commands as you want to execute on different version of applications.

Upvotes: 1

Related Questions