Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7687091
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T19:37:28+00:00 2026-05-31T19:37:28+00:00

I have an existing database based on SQLiteOpenHelper that has several versions and code

  • 0

I have an existing database based on SQLiteOpenHelper that has several versions and code to upgrade it and that works fine. But in case a user installs an older version of the app (that expects a lower database version) it will currently crash – the ContentProvider using it can’t access the database. I’d like to prevent it from crashing but I don’t want to actually downgrade the database – adding the code to do that would be pain. Dropping all tables would certainly work but starting with a fresh file is imo cleaner and less error prone.

That’s about what the database helper looks like – nothing special

public class MyDbHelper extends SQLiteOpenHelper {
    private static final int DATABASE_VERSION = 3;
    private static final String DATABASE_NAME = "my.db";

    public MyDbHelper(Context context) {
        super(context, DATABASE_NAME, null, DATABASE_VERSION);
    }

    @Override
    public void onCreate(SQLiteDatabase db) {
        onUpgrade(db, 0, DATABASE_VERSION);
    }

    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
        if (newVersion < 1) db.execSQL("CREATE TABLE A...");
        if (newVersion < 2) db.execSQL("CREATE TABLE B...");
        if (newVersion < 3) db.execSQL("CREATE TABLE C...");
    }

    @Override
    public void onDowngrade(SQLiteDatabase db, int oldVersion, int newVersion) {
        // I'd like to delete the database here
        // the only problem is that I can't from here
        // since this is called in the middle of getWritableDatabase()
        // and SQLiteDatabase has no .recreate() method.
    }
}

The possible ways I’ve come up to do that are:

  • Do it from the outside: catch exceptions in the ContentProvider, delete the file and request to open the database again. – I don’t like that since it’s not the responsibility of the provider.
  • Replacing SQLiteOpenHelper with my own copy of that class that deletes the file instead of calling onDowngrade – Problem is that it’s using package private parts of SQLiteDatabase (e.g. .lock()) which I can’t replace without duplicating SQLiteDatabase too (that would probably result in duplicating the whole sqlite stack).

Is there any good approach to do that or do I have to go the DROP TABLES way e.g. like described here?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-31T19:37:30+00:00Added an answer on May 31, 2026 at 7:37 pm

    I’ve figured out a way that works nicely by extending SQLiteOpenHelper and all I need to do in MyDbHelper is to extend this class.

    public abstract class DeletingSQLiteOpenHelper extends SQLiteOpenHelper {
        private static final String TAG = DeletingSQLiteOpenHelper.class.getSimpleName();
    
        private final File mDatabaseFile;
    
        public DeletingSQLiteOpenHelper(Context context, String name, CursorFactory factory, int version,
                DatabaseErrorHandler errorHandler) {
            super(context, name, factory, version, errorHandler);
            mDatabaseFile = context.getDatabasePath(name);
        }
    
        public DeletingSQLiteOpenHelper(Context context, String name, CursorFactory factory, int version) {
            super(context, name, factory, version);
            mDatabaseFile = context.getDatabasePath(name);
        }
    
        @Override
        public synchronized SQLiteDatabase getWritableDatabase() {
            try {
                return super.getWritableDatabase();
            } catch (SQLiteDowngradeFailedException e) {
                // that's our notification
            }
    
            // try to delete the file
            mDatabaseFile.delete()
    
            // now return a freshly created database
            return super.getWritableDatabase();
        }
    
        @Override
        public final void onDowngrade(SQLiteDatabase db, int oldVersion, int newVersion) {
            // throwing a custom Exception to catch it in getWritableDatabase
            throw new SQLiteDowngradeFailedException();
        }
    
        // that's the exception
        static class SQLiteDowngradeFailedException extends SQLiteException {
            public SQLiteDowngradeFailedException() {}
    
            public SQLiteDowngradeFailedException(String error) {
                super(error);
            }
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have an existing database that has some testing data into and I'm interested
I'm working on a PHP-based webapp that has an existing MySQL database where all
I have an existing database of a film rental system. Each film has a
I have an existing database in mysql. One of my tables has discontinuous ids.
If I have an existing database, I want to be able to automatically code
I have an existing SQL Server 2005 database that runs our accounting/inventory application. We
Have an existing C# .NET code base that is currently coded to directly access
I need to generate annotation based entity from the existing database in spring.I have
I see that EF can update a model based on an existing database schema.
I have an existing, non-framework-based PHP/MySQL website. It has a simple security model, with

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.