使用会议室数据库(会议室持久性库)创建触发器 [英] Create trigger using Room Database(Room Persistence Library)

查看:86
本文介绍了使用会议室数据库(会议室持久性库)创建触发器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何使用Room Persistence库"创建触发器"

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

推荐答案

RoomDatabase上调用getOpenHelper().这为您提供了SupportSQLiteOpenHelper,其API让人联想到SQLiteOpenHelper.在那儿,调用getWritableDatabase()获得一个SupportSQLiteDatabase,然后在那儿使用execSQL()执行您的SQL语句.正如AdamMc331在此Kotlin代码段中所示,RoomDatabase.Callback是执行这种SQL的地方.

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在这种情况下并没有真正的帮助,但是对于这种情况,您始终可以使用低级数据库API.

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.

这篇关于使用会议室数据库(会议室持久性库)创建触发器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆