Created
November 3, 2019 05:15
-
-
Save OmarAlbelbaisy/a40e77a5aaa8c0c277a6a2e598868d1c to your computer and use it in GitHub Desktop.
Custom SQLiteOpenHelper class to use external pre-populated database file
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | |
} | |
} |
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
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);