How to use preload2 in Android?
Answers
1) Put the “android-sqlite-asset-helper.jar” to the “libs” folder. Make sure it is included in the build path.
(The jar file can be downloaded from Android SQLiteAssetHelper)
2) Create a new class “DBHelper.java” (Remember to change DATABASE_NAME to your own one.)
1
2
3
4
5
6
7
8
public class DBHelper extends SQLiteAssetHelper {
private static final String DATABASE_NAME = "data.db";
private static final int DATABASE_VERSION = 1;
public DBHelper(Context context) {
super(context, DATABASE_NAME, null, DATABASE_VERSION);
}
}
3) Zip compress the database file as “data.db.zip” (use your own “database file name.zip”)
4) Put it in “assets/databases/” of your android project.
5) Done. You can get the “SQLiteDatabase” object in your Activity by
1
2
DBHelper dbHelper = new DBHelper(this);
dbHelper.getReadableDatabase();
Something more to explore
You may have noticed from DBHelper, DATABASE_VERSION is set to 1. The helper library does support upgrading database, for more details, please visit the library’s repository.