-
-
Save RohitSurwase/face9fbc1d4debd20077e37447349167 to your computer and use it in GitHub Desktop.
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
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) { | |
super.onConfigure(db); | |
db.setForeignKeyConstraintsEnabled(true); | |
} */ | |
@Override | |
public void onCreate(SQLiteDatabase db) { | |
//Create and Execute Create Table Query } | |
@Override | |
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