How can I embed an SQLite database into an application? How can I embed an SQLite database into an application? sqlite sqlite

How can I embed an SQLite database into an application?


I solved that problem by:

  1. adding file.db into project/assets folder;

  2. writing next class:

    public class LinnaeusDatabase extends SQLiteOpenHelper{    private static String DATABASE_NAME = "Dragonfly.db";    public final static String DATABASE_PATH = "/data/data/com.kan.linnaeus/databases/";    private static final int DATABASE_VERSION = 1;    private SQLiteDatabase dataBase;    private final Context dbContext;    public LinnaeusDatabase(Context context) {        super(context, DBActivity.DatabaseName, null, DATABASE_VERSION);        this.dbContext = context;        DATABASE_NAME = DBActivity.DatabaseName;        // checking database and open it if exists        if (checkDataBase()) {            openDataBase();        } else        {            try {                this.getReadableDatabase();                copyDataBase();                this.close();                openDataBase();            } catch (IOException e) {                throw new Error("Error copying database");            }            Toast.makeText(context, "Initial database is created", Toast.LENGTH_LONG).show();        }    }    private void copyDataBase() throws IOException{        InputStream myInput = dbContext.getAssets().open(DATABASE_NAME);        String outFileName = DATABASE_PATH + DATABASE_NAME;        OutputStream myOutput = new FileOutputStream(outFileName);        byte[] buffer = new byte[1024];        int length;        while ((length = myInput.read(buffer))>0){            myOutput.write(buffer, 0, length);        }        myOutput.flush();        myOutput.close();        myInput.close();    }    public void openDataBase() throws SQLException {        String dbPath = DATABASE_PATH + DATABASE_NAME;        dataBase = SQLiteDatabase.openDatabase(dbPath, null, SQLiteDatabase.OPEN_READWRITE);    }    private boolean checkDataBase() {        SQLiteDatabase checkDB = null;        boolean exist = false;        try {            String dbPath = DATABASE_PATH + DATABASE_NAME;            checkDB = SQLiteDatabase.openDatabase(dbPath, null,            SQLiteDatabase.OPEN_READONLY);        } catch (SQLiteException e) {            Log.v("db log", "database does't exist");        }        if (checkDB != null) {            exist = true;            checkDB.close();        }        return exist;    }}


Nice article on ReignDesign blog titled Using your own SQLite database in Android applications. Basically you precreate your database, put it in your assets directory in your apk, and on first use copy to "/data/data/YOUR_PACKAGE/databases/" directory.


I've done this in the past by storing a JSON file in the application in the res/raw resources and then loading the file on first load. From there, you can use the bulk insert mode to batch-import entries. See my database loader for Units as an example of this technique. One benefit of the res/raw style is that you can use the Android resource selecting system to localize the data to different regions, so you could have a different database (or part thereof) for different languages or regions.

You could also put a raw SQL dump in a similar file and load that on first load. You can get the file from the resources by using openRawResource(int). I'd recommend this instead of storing a pre-made sqlite database file to increase compatibility between versions of sqlite, as well as make maintaining the database easier (from an app-development lifecycle POV).

When loading things in bulk, make sure to use transactions, as that'll help speed things up and make the loading more reliable.