Skip to content

Instantly share code, notes, and snippets.

@milaptank
Created March 6, 2018 08:58
Show Gist options
  • Save milaptank/7094e677643e326f815131a1fa771f1b to your computer and use it in GitHub Desktop.
Save milaptank/7094e677643e326f815131a1fa771f1b to your computer and use it in GitHub Desktop.
package com.core.storage;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.database.sqlite.SQLiteStatement;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
public class DatabaseUtil {
private static DatabaseHelper mOpenHelper = null;
private static Context context;
private static final String DATABASE_RESOURCE_FILE_NAME = "database.sql";
/**
* Initialize the DatabaseUtil instance for the app
*
* {@link Context}
*
* databasename
*/
public static void init(Context mcontext, String databasename, int version, DatabaseConfig config) {
if (mOpenHelper == null) {
context = mcontext;
mOpenHelper = new DatabaseHelper(mcontext, databasename, version, config);
try {
mOpenHelper.getWritableDatabase().close();
}catch (Exception e) {
}
}
}
/**
* this method is used to get the current database instance
*
* @return SQLiteDatabase instance to access current database
* @throws DbException
*/
public static SQLiteDatabase getDatabaseInstance() throws DbException {
if (mOpenHelper == null) throw new DbException("DatabaseUtil.init() is not called.");
return mOpenHelper.getWritableDatabase();
}
/**
* Use this static method to release database related resource. Pass
* <code>null</code> if not particular argument is not applicable.
*
* @param
* {@link SQLiteDatabase}
* @param stmt
* {@link }
* @param cursor
* {@link Cursor}
*/
public static void closeResource(SQLiteStatement stmt,
Cursor cursor) {
if (stmt != null) {
try {
stmt.close();
} catch (Exception e) {
}
}
if (cursor != null) {
try {
cursor.close();
} catch (Exception e) {
}
}
}
/**
* This class helps open, create, and upgrade the database file.
*/
public static class DatabaseHelper extends SQLiteOpenHelper {
DatabaseConfig config = null;
public DatabaseHelper(Context context, String datbaseName, int version, DatabaseConfig config) {
super(context, datbaseName, null, version);
this.config = config;
}
@Override
public void onCreate(SQLiteDatabase db) {
executeStatement(db);
if(config != null) {
try {
config.onCreate(db);
} catch (Exception e) {
}
}
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
if(config != null) {
try {
config.onUpgrade(db);
} catch (Exception e) {
}
}
onCreate(db);
}
}
/**
* executes the statements from the file which is in the asset folder
* if line in the file starts with # then it consider it as the comments
* @param db
*/
private static void executeStatement(SQLiteDatabase db)
{
try {
InputStream inputStream = context.getAssets().open(DATABASE_RESOURCE_FILE_NAME);
if(inputStream != null) {
BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream));
String statement;
while((statement = reader.readLine()) != null) {
if(statement.trim().length() > 0 && !statement.startsWith("#")) {
db.execSQL(statement);
}
}
}
} catch (IOException e) {
e.printStackTrace();
}
}
/**
* Delete Database and releases all resources.
*/
public static void deleteDatabase(String dbName) {
if(mOpenHelper != null) {
mOpenHelper.close();
mOpenHelper = null;
context.deleteDatabase(dbName);
context = null;
}
}
/**
* Close DatabaseUtil and releases all resources.
*/
public static void destroy() {
if(mOpenHelper != null) {
mOpenHelper.close();
mOpenHelper = null;
context = null;
}
}
public static DatabaseHelper getmOpenHelper() {
return mOpenHelper;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment