Search code examples
androiddatabasesqliteupdatesassets

How can I replace (overwrite the entire file) a sqlite database provided by SQLiteAssetHelper?


I have a database file in my app's src/main/assets/databases directory. I want to download a new copy of the entire database (no data needs to be retained), and overwrite the old one. How can I do this with SQLiteAssetHelper?

My understanding is that SQLiteAssetHelper checks if the database is "installed" on the device, and if not, pulls it out of the src/main/assets/databases directory and copies it somewhere that android can use. Since I can't modify my app's assets, where does SQLiteAssetHelper copy the database, and what's the proper way to overwrite it?

Here's my extended SQLiteAssetHelper, in case it's of any use:

import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import com.readystatesoftware.sqliteasset.SQLiteAssetHelper;

public class EnumDatabase extends SQLiteAssetHelper {

    private static final String DATABASE_NAME = "enums.sqlite3";
    private static final int DATABASE_VERSION = 1;

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

}

Solution

  • How can I do this with SQLiteAssetHelper?

    Delete the old database, then call getReadableDatabase() or getWriteableDatabase() on your SQLiteAssetHelper. You can call deleteDatabase() on any handy Context to delete the existing database. Make sure your database is closed first, though.