Page Banner

Enable ON DELETE CASCADE in SqLite, Android

If you are using SqLite for the first time in Android and want to create a relational database in it you will be shocked knowing that ON DELETE CASCADE does not work. Yes, this is the default behaviour of Sqlite in Android. By default SqLite does not support foreign key. To enable it you have to pass a PRAGMA command each time you open a database.

Write PRAGMA foreign_keys = ON; inside the onOpen method of your database helper class.

public void onOpen(SQLiteDatabase db) {
db.execSQL("PRAGMA foreign_keys = ON;");