Skip to content

Instantly share code, notes, and snippets.

@RohitSurwase
Created October 5, 2017 15:29
Show Gist options
  • Save RohitSurwase/face9fbc1d4debd20077e37447349167 to your computer and use it in GitHub Desktop.
Save RohitSurwase/face9fbc1d4debd20077e37447349167 to your computer and use it in GitHub Desktop.
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