Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
652 views
in Technique[技术] by (71.8m points)

android - Create trigger using Room Database(Room Persistence Library)

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
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

Call getOpenHelper() on your RoomDatabase. This gives you a SupportSQLiteOpenHelper, which has an API reminiscent of SQLiteOpenHelper. On there, call getWritableDatabase() to get a SupportSQLiteDatabase, and on there use execSQL() to execute your SQL statements. A RoomDatabase.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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...