Because my new version app is to generate new database. But old version same data which causing the issue.
So I need to uninstall old version maintain current version only .May I know what is the correct way to achieve my objective?
Because my new version app is to generate new database. But old version same data which causing the issue.
So I need to uninstall old version maintain current version only .May I know what is the correct way to achieve my objective?
Implement the onUpgrade method in your SQLiteHelper subclass.
Database Upgrading
The constructor of your implementation of SQLiteOpenHelper should call the super constructor, passing along the database name and version. The onUpgrade() method will only be called when the version integer is larger than the current version running in the emulator. If you want the onUpgrade() method to be called, you need to increment the version number in your code.
The SQLiteHelper code keeps the VERSION field you pass into the constructor, and saves it in the database. When you need to change your schema, you increment this integer.
After you upgrade
the application such that the version number in the app is higher than the version number in the database on "disk"
, the SQLiteHelper code notices, and calls the +onUpgrade()+
method with the old and new version numbers.
You are expected to know what changed between each version, and update
the database schema accordingly.
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
// Your Staff
onCreate(db);
}
Check SQLiteOpenHelper onUpgrade() Confusion Android
Please read Official Guide.
int, int)
There is no way to uninstall app programmatically before install new until user manually uninstall app before install new one.
The best solution is to upgrade your database, in an onUpgrade
method of your sqlite helper class. Using this you can delete, update table & table data(whatever you want to do!).
If you use the Sqlite Database then use the onUpgrade method for change database without uninstall old version app.
Please check link:- https://thebhwgroup.com/blog/how-android-sqlite-onupgrade
If your old version of app is already having the database and you have change the database structure in new version then you should write proper code to update db in "onUpgrade" method of SQLiteOpenHelper.
To get call onUpgrade you need to increase version of of your database. If you change the version number then android identifies your database is changed so it will gona called onUpgrade method of SQLiteOpenHelper and in onUpgrade method you can write code to change DB structure either by deleting old tables or modifying old tables to new structure.
It is best practice for database versioning and you should follow this. Please refer here for more details about how to upgrade database.