The Classic Solution: SQLiteAssetHelper
The recommended “something” for traditional SQLite work in Android has been Jeff Gilfelt’s SQLiteAssetHelper
. Basic use of SQLiteAssetHelper
is fairly simple:
- Create an
assets/databases/
directory in yourmain
sourceset of your app module - Put your pre-populated database in that directory, with the same filename that you want to use at runtime
- Rather than using
SQLiteOpenHelper
, subclassSQLiteAssetHelper
instead, supplying that filename:
public class YourDatabase extends SQLiteAssetHelper {
private static final String DB_NAME="whatever.db";
private static final int SCHEMA_VERSION=1;
public YourDatabase(Context context) {
super(context, DB_NAME, null, SCHEMA_VERSION);
}
}
The rest of your code can use your SQLiteAssetHelper
subclass just as it would SQLiteOpenHelper
, such as calling getReadableDatabase()
or getWritableDatabase()
. The first time one of those methods is called, SQLiteAssetHelper
will notice that there is no database and will copy the database from assets into the proper location.
NOTE: Jeff Gilfelt is no longer maintaining SQLiteAssetHelper
. Right now, it still works fine for this role. With luck, new actively-supported solutions will become available.
Prev Table of Contents Next
This book is licensed under the Creative Commons Attribution-ShareAlike 4.0 International license.