Reputation: 171
I have preloaded sqlite database located in assets, which is copied to default database location after app first installation. I'd like to do this procedure on each app update. I don't need to keep any user data from existing database.
How to delete old database file, when app is being updated?
Upvotes: 3
Views: 2174
Reputation: 1074
These are actually two questions:
You could save a variable with the last version code in a Shared Preferences. After each start of the application you check your current code with the saved code. If they don't match, it was an update and you execute some code.
If the database is closed, you can simply delete the file. Use Context.getDatabasePath(String) to find the location of the file, and File.delete() for deleting it. Make sure no process is accessing the database file. If so, you might need to use the File.deleteOnExit() and force a restart of the application.
Upvotes: 3
Reputation: 29642
Use onUpgrade()
method of SQLiteOpenHelper
class.
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
// Delete old table
db.execSQL("DROP TABLE IF EXISTS" + TABLE_NAME);
// Re-create table
onCreate(db);
}
Upvotes: 0