Skip to content

Instantly share code, notes, and snippets.

@OmarAlbelbaisy
Created November 3, 2019 05:15
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save OmarAlbelbaisy/a40e77a5aaa8c0c277a6a2e598868d1c to your computer and use it in GitHub Desktop.
Save OmarAlbelbaisy/a40e77a5aaa8c0c277a6a2e598868d1c to your computer and use it in GitHub Desktop.
Custom SQLiteOpenHelper class to use external pre-populated database file
import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
public class ExternalDatabaseHelper extends SQLiteOpenHelper {
private static final String DB_NAME = "data.db";
private static final int DB_VERSION = 1;
/**
* Prepare and initialize the database
* @param context application content
*/
public static ExternalDatabaseHelper getInstance(Context context) {
File databaseFile = context.getDatabasePath(DB_NAME);
// Copy database file if not exist
if (!databaseFile.exists()) {
copyDatabaseFile(context, databaseFile);
} else {
// If the current database is old, delete the database and copy new one
SQLiteDatabase db = SQLiteDatabase.openDatabase(databaseFile.getAbsolutePath(), null, 0);
if (db.needUpgrade(DB_VERSION)) {
if (databaseFile.delete()) {
copyDatabaseFile(context, context.getDatabasePath(DB_NAME));
}
}
}
return new ExternalDatabaseHelper(context);
}
/**
* Copy database file from assets to app databases folder
* @param context application context
* @param databaseFile target database file
*/
private static void copyDatabaseFile(Context context, File databaseFile) {
try {
InputStream input = context.getAssets().open(DB_NAME);
OutputStream output = new FileOutputStream(databaseFile);
byte[] buffer = new byte[1024];
int length;
while ((length = input.read(buffer)) > 0) {
output.write(buffer, 0, length);
}
output.flush();
output.close();
input.close();
} catch (IOException mIOException) {
throw new Error("Error Copying DataBase");
}
}
/**
* Constructor to initialize database helper
* @param context application context
*/
private ExternalDatabaseHelper(Context context) {
super(context, DB_NAME, null, DB_VERSION);
}
@Override
public void onCreate(SQLiteDatabase db) {
// Nothing here
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
// Nothing here
}
}
@OmarAlbelbaisy
Copy link
Author

To read the database file from raw folder instead of assets
Just replace
InputStream input = context.getAssets().open(DB_NAME);
with
InputStream input = context.InputStream raw = getResources().openRawResource(R.raw.data);

@OmarAlbelbaisy
Copy link
Author

To use this class

ExternalDatabaseHelper externalDatabaseHelper = ExternalDatabaseHelper.getInstance(context);
SQLiteDatabase database = externalDatabaseHelper.getWritableDatabase();

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment