I came across another piece of code regarding the inclusion of a foreign key constraint in SQLiteHelper. I was wondering if I should include a foreign key constraint in onOpenor onConfigureif I want to support API <16.
This discussion assumes that onOpenthis is the right place before API 16: Android foreign key constraints using SQLite? on Remove cascade
However, since API 16, the white paper mentions onConfigureis the right place.
public void setForeignKeyConstraintsEnabled (boolean enable)
...
A good time to call this method is right after calling openOrCreateDatabase(File, SQLiteDatabase.CursorFactory) or in the onConfigure(SQLiteDatabase) callback.
Can I find out what a single entry point is for both API 16 and 16?
@Override
public void onOpen(SQLiteDatabase database) {
super.onOpen(database);
if (!database.isReadOnly()) {
db.execSQL("PRAGMA foreign_keys=ON;");
}
}
or
@SuppressLint("NewApi")
@Override
public void onConfigure(SQLiteDatabase database) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) {
database.setForeignKeyConstraintsEnabled(true);
} else {
database.execSQL("PRAGMA foreign_keys=ON");
}
}
source
share