I suggest the SQLiteAssetHelper
library from:
It has become the standard for this technique, and does all the hard work for you.
And integration into your project is as simple as including 1 jar file.
For example, here is my database helper class - as you can see it inherits from SQLiteAssetHelper
, but there is no code I had to write in order to get the database copied from the /assets
directory. That is all handled for me.
public class DbHelper extends SQLiteAssetHelper
{
private static final String DATABASE_NAME = "NameOfDbFile.sqlite";
private static final int DATABASE_VERSION = 2;
public DbHelper(Context context)
{
super(context, DATABASE_NAME, null, DATABASE_VERSION);
// delete old database when upgrading
setForcedUpgrade(DATABASE_VERSION);
}
// ... and below a basic query as if it was any old SQLiteOpenHelper subclass
public Cursor getUsers()
{
SQLiteDatabase db = getReadableDatabase();
SQLiteQueryBuilder qb = new SQLiteQueryBuilder();
qb.setTables(User.TABLE);
Cursor c = qb.query(db, User.COLUMNS, null, null, null, null, null);
c.moveToFirst();
return c;
}
// etc...
}
I was hesitant at first to use a library for this, but it is really simple to use.
However, if you want to redo the work yourself, then you can take a look at how this library does it, since it is open-source on Github.