Should I include a foreign key constraint in onOpen or onConfigure

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()) {
        // Enable foreign key constraints 
        db.execSQL("PRAGMA foreign_keys=ON;");
    } 
} 

or

// https://stackoverflow.com/questions/13641250/sqlite-delete-cascade-not-working
@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");
    }
}
+4
source share
1

onConfigure() , API 16 . minSdkVersion 16 , .

onOpen() , onCreate()/onUpgrade() .. SQL onCreate()/onUpgrade(), , onOpen(). minSdkVersion < 16 :

  • onOpen().

  • onCreate()/onUpgrade() , . , , , .

+9

Source: https://habr.com/ru/post/1534486/


All Articles