Ilja
Ilja

Reputation: 46509

How to invoke onUpgrade() in Android, for work with sqlite database

I'm following some articles about database manipulations (sqlite) in android, and came across a problem which I can't find solution for.

Essentially I want database to be recreated every time I run application (Testing purposes).

Upvotes: 1

Views: 483

Answers (2)

danny117
danny117

Reputation: 5651

to recreate the database uninstall the app from the manage apps on the device. But to test onupgrade code change the constant for the database version to something higher in the constructor for the SQLiteOpenHelper.

//private static final int DATABASE_VERSION = 45;
//private static final int DATABASE_VERSION = 46;
private static final int DATABASE_VERSION = 47;

public RidesDatabaseHandler(Context context) {
    super(context, DATABASE_NAME, null, DATABASE_VERSION);
    this.context = context;
}

and it will automatically call the onUpgrade method. Speaking of onUpgrade check out this Adams Upgrade Method

Upvotes: 0

Marco Acierno
Marco Acierno

Reputation: 14847

If you want to upgrade the database everytime you could try to call onUpgrade manuallly in onCreate everytime and add a boolean variable to avoid infinite recursive calls. (it's debug code, so you should mark it with a // todo comment or something similar which alerts you.)

Or, make a method called destroyDB and add it after

DatabaseHandler database = new DatabaseHandler(this);

which does the same thing i said above (calls onUpgrade) but you avoid the infinity recursive calls and if you don't remember this when you see destroyDB you will check what this method does.

Or change version manually everytime you need to check database.

Upvotes: 1

Related Questions