Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
singletone DBHelper
public class DBHelper {
private static DBHelper dBHelperInstance;
private CreateTable createTable; // CreateTable is extended SQLiteOpenHelper class
private SQLiteDatabase database;
private int count = 0;
// this method should call in application class
public static synchronized void init(Context context) {
if (dBHelperInstance == null) {
dBHelperInstance = new DBHelper(context);
}
}
private DBHelper(Context context) {
createTable = new CreateTable(context);
}
public static synchronized DBHelper getInstance() {
if (dBHelperInstance == null) {
throw new IllegalStateException(DBHelper.class.getSimpleName() +
" is not initialized, call init(..) method first.");
}
return dBHelperInstance;
}
public synchronized void openDatabase() {
count++;
if (count == 1) {
database = createTable.getWritableDatabase();
}
}
public synchronized void closeDatabese() {
count--;
if (count == 0) {
createTable.close();
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.