Search code examples
androidsqliteandroid-contentprovider

ReCreate Content provider after deletion


I want to delete my sqlite db in the application due to some reason:

this.deleteDatabase(contentProvider.getDatabaseName());

I want to recreate my database file after that.

I read in the documentation that :

onCreate is called for all registered content providers on the application main thread at application launch time.

And this is what I am doing to Create db in OnCreate at launch Time:

@Override
    public boolean onCreate() {
        try {
            contentProviderHelp = new ContentProviderHelp();

            userInfoTable = new UserInfoTable(contentProviderHelp);
            employeeInfoTable = new EmployeeInfoTable(contentProviderHelp);
            productsTable = new ProductsTable(contentProviderHelp);

            db = new DbMetadata(userInfoTable, employeeInfoTable, productsTable);
            ddlHelp = new DDLHelper(db);

            matcher = new UriMatch(userInfoTable, employeeInfoTable, productsTable, new UriMatcher(UriMatcher.NO_MATCH));

            dbHelp = new DatabaseHelper(getContext(), DATABASE_NAME, DATABASE_VERSION, ddlHelp);
            projectionMaps = new ProjectionHelper(db);
            return true;
        } catch (Exception e) {
            return false;
        }
    }

Can you help me how can I recreate ContentProvider after deletation of database file?


Solution

  • Just create a static function in you DatabaseHelper for this case and provide the current context as an argument. In your DatabaseHelper you can recreate the database