4

Very new to Java. Working with my app, and decided, I'd drop my DATABASE_VERSION back down to 1 (no real reason).

When I started the app, it crashed, and one of the errors was:

E/AndroidRuntime(14905): Caused by: android.database.sqlite.SQLiteException: Can't downgrade database from version 17 to 4

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

I already have an onUpgrade(), where I delete the tables then runs the onCreate()... so I thought I'd make an onDowngrade():

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

But apparently that's not a superclass method that I can override.

Is there a way (easy way hopefully) to allow me to change the database version to any number I want and have it still run without crashing immediately?

Dave
  • 28,833
  • 23
  • 113
  • 183
  • 2
    Please note that onDowngrade was introduced in API level 11. If you project has a build target lower that 11 you won't see it. Anyway, i can't see any reason to handle db downgrade. – rciovati Jul 06 '12 at 16:16

1 Answers1

10

Is there a way (easy way hopefully) to allow me to change the database version to any number I want and have it still run without crashing immediately?

In development, simply get rid of your database, either via the Clear Data button for your app in Settings, or by uninstalling the app from settings, or by wiping the whole emulator (if this is an emulator). Your database will be created anew on next run via a call to onCreate() in your SQLiteOpenHelper.

In production, as rciovati noted, the concept of supporting a downgrade was only introduced with API Level 11, and I have never tried it personally.

CommonsWare
  • 986,068
  • 189
  • 2,389
  • 2,491
  • CommonsWare@ is there any other work around for this other clearing the cache of the data. I mean are u suggest any code change? – coder_15 May 19 '13 at 19:56
  • Your help will be greatly appreciated. – coder_15 May 19 '13 at 19:57
  • @VenkatGottipati: If your `android:minSdkVersion` is 11 or higher, override `onDowngrade()` in your `SQLiteOpenHelper` and add your logic there. Otherwise, never downgrade. – CommonsWare May 19 '13 at 20:01
  • we are receiving lot of customer contacts for silk browser crashing in kindle fire hd on same issue. I checked the stack trace it shows the same exception as above like "android.database.sqlite.SQLiteException: Can't upgrade read-only database from version 29 to 27". You can clear the data to resolve the issue because the database will be recreated. but it is best that we do not require the user the clear the data. because the users may have some bookmarks they do not want to clear. Can you provide any sample code for this. – coder_15 May 19 '13 at 20:10
  • Below is the Code snippet from SQLiteOpenHelper.java throwing the SQLiteException: try { mIsInitializing = true; String path = mContext.getDatabasePath(mName).getPath(); db = SQLiteDatabase.openDatabase(path, mFactory, SQLiteDatabase.OPEN_READONLY, mErrorHandler); if (db.getVersion() != mNewVersion) { throw new SQLiteException("Can't upgrade read-only database from version " + db.getVersion() + " to " + mNewVersion + ": " + path); } – coder_15 May 19 '13 at 20:14
  • @VenkatGottipati: The exceptions are not the same. You can tell that by reading, as "upgrade" and "downgrade" are different words. Open your database using `getWriteableDatabase()` instead of `getReadableDatabase()` to avoid the specific exception you are encountering. – CommonsWare May 19 '13 at 20:16
  • I agree exceptions are different. If some problem, such as a full disk, requires the database to be opened read-only rite.So at that situations what we can do. Sample code snippet is really appreciated. :) – coder_15 May 19 '13 at 20:24
  • Actually this is a wide spread issue. So we are considering different possibilities – coder_15 May 19 '13 at 20:26
  • @VenkatGottipati: " If some problem, such as a full disk, requires the database to be opened read-only" -- do not use `SQLiteOpenHelper` in that scenario. Open the database directly with `SQLiteDatabase`. Either that, or do not downgrade the database. If you wish additional assistance with your issues, please open a new StackOverflow question, as your concerns have nothing to do with this question. – CommonsWare May 19 '13 at 20:34