1

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?

IntelliJ Amiya
  • 74,896
  • 15
  • 165
  • 198
Vinayagam.D
  • 322
  • 3
  • 10
  • 4
    Possible duplicate of [SQLiteOpenHelper onUpgrade() Confusion Android](http://stackoverflow.com/questions/3505900/sqliteopenhelper-onupgrade-confusion-android) – Aniruddha K.M Jan 06 '16 at 06:18

4 Answers4

4

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.

http://developer.android.com/intl/es/reference/android/database/sqlite/SQLiteOpenHelper.html#onUpgrade(android.database.sqlite.SQLiteDatabase,

int, int)

Community
  • 1
  • 1
IntelliJ Amiya
  • 74,896
  • 15
  • 165
  • 198
1

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

Check this answer & this page how to upgrade

Community
  • 1
  • 1
Dhaval Parmar
  • 18,812
  • 8
  • 82
  • 177
0

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

Kaushal Patel
  • 338
  • 4
  • 19
0

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.

VikasGoyal
  • 3,308
  • 1
  • 22
  • 42