Skip to content

Instantly share code, notes, and snippets.

What would you like to do?
class DatabaseHelper extends SQLiteOpenHelper {
/*This Constructor should be private to prevent direct instantiation. */
private DatabaseHelper(Context context) {
super(context, DATABASE_NAME, null, DATABASE_VERSION);
/* Here I have created synchronized function which will ensure only one instance of Helper class will exists */
public static synchronized DatabaseHelper getInstance(Context context) {
// pass application context here
if (sInstance == null) {
sInstance = new DatabaseHelper(context.getApplicationContext());
} return sInstance; }
//Call following function if you want to enable foreign key support
/* @Override
public void onConfigure(SQLiteDatabase db) {
} */
public void onCreate(SQLiteDatabase db) {
//Create and Execute Create Table Query }
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
if (oldVersion != newVersion) {
/* Drop all old tables and recreate them by calling onCreate(db) */ }}}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment