Search code examples
javaandroidsqliteandroid-sqlitedatabase-versioning

How onUpgrade() works if anyone uninstall and cleared all data?


I'm working on an android app for some days using SQLite as databse. Sometimes I had to add column. For this reason, I just change database version and write down the changes in onUpgrade() like this:

db.execSQL("ALTER TABLE "+TABLE_EMPLOYEE+" ADD "+COLUMN_EMPLOYEE_MOBILE+" TEXT");

It works for when the app is still installed in the mobile. But it shows error when the app is not installed before or uninstalled and cleared all the data and cache. Because when checking version, it got only one version. How to resolve this?


Solution

  • Your onCreate() should set up a new database appropriate for your current database version. onUpgrade() is only invoked when there's a database file around with an older version.