OnDowngrade () in SQLiteOpenHelper (or another way not crashing on downgrade)

Very new to Java. By working with my application and deciding, I would reset my DATABASE_VERSION to 1 (for no real reason).

When I launched the application, it crashed, and one of the errors:

E / AndroidRuntime (14905): called: android.database.sqlite.SQLiteException: cannot downgrade the database from version 17 to 4

E / AndroidRuntime (14905): with android.database.sqlite.SQLiteOpenHelper.onDowngrade (SQLiteOpenHelper.java:307)

I already have onUpgrade() where I delete the tables and then run onCreate() ... so I thought I would do onDowngrade() :

 @Override public void onDowngrade(SQLiteDatabase db, int oldVersion, int newVersion) { onUpgrade(db); } 

But apparently this is not a superclass method that I can override.

Is there a way (hopefully) so that I can change the database version to any number I want and run it without crashing right away?

+4
java android sqlite
source share
2 answers

Is there a way (hopefully) so that I can change the database version to any number I want and run it without crashing right away?

In development, simply get rid of your database either using the "Clear Data" button for your application in the "Settings", or by removing the application from the settings, or by cleaning the entire emulator (if it is an emulator). Your database will be recreated in the next run through the onCreate() call in SQLiteOpenHelper .

In production, as rciovati noted, the concept of downgrade support was introduced only from API level 11, and I never tried it personally.

+10
source share

download sqlite db file from Android DeviceFileExplorer

[data / data / name of your package / database]

open file with ( DB Browser for SQLite )

go to the " Edit pragmas " tab and downgrade the database version from the user version

finally save the file and upload it to DeviceFileExplorer

enter image description here

0
source share

All Articles