Creating a trigger using a room database (space save library)

How to " Create a trigger " using the save space 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
+6
source share
1 answer

Call getOpenHelper()on RoomDatabase. It gives you SupportSQLiteOpenHelperthat has an API reminiscent of SQLiteOpenHelper. In this case, call getWritableDatabase()to get SupportSQLiteDatabase, and use there execSQL()to execute your SQL statements. A RoomDatabase.Callbackis one place to run this kind of SQL, as AdamMc331 illustrates Kotlin in this snippet .

IOW, Room , API , .

+6

All Articles