How to "Create Trigger" using Room Persistence library
CREATE TRIGGER IF NOT EXISTS delete_till_10 INSERT ON user WHEN (select count(*) from user)>9
BEGIN
DELETE FROM user WHERE id IN (SELECT id FROM user ORDER BY id limit (select count(*) -9 from user));
END
Call
getOpenHelper()
on yourRoomDatabase
. This gives you aSupportSQLiteOpenHelper
, which has an API reminiscent ofSQLiteOpenHelper
. On there, callgetWritableDatabase()
to get aSupportSQLiteDatabase
, and on there useexecSQL()
to execute your SQL statements. ARoomDatabase.Callback
is one place to execute this sort of SQL, as AdamMc331 illustrates in this Kotlin snippet.IOW, Room does not really help with this scenario, but you can always work with the lower-level database API for cases like this one.