简体   繁体   中英

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).

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.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM