android room documentation声明 PRAGMA recursive_triggers 默认启用:
By default, all RoomDatabases use in memory storage for TEMP tables and enables recursive triggers.
当我将插入与“onConflict(REPLACE)”一起使用时,这会给我带来问题:如果(且仅当)启用 recursice_triggers,这会触发我的 onDelete 触发器(请参阅 SQLite Documentation):
REPLACE [...] When the REPLACE conflict resolution strategy deletes rows in order to satisfy a constraint, delete triggers fire if and only if recursive triggers are enabled.[...]
我试图通过在 RoomDatabase.Callback.onCreate() 中添加 db.execSQL("PRAGMA recursive_triggers = 0;");
来禁用它,但这没有任何效果。
public static synchronized FnsDatabase getInstance(Context context){
if (instance == null) {
instance = Room.databaseBuilder(context, MyDb.class, "mydb.db")
.addCallback(triggerCallback)
.build();
}
return instance;
}
private static RoomDatabase.Callback triggerCallback = new RoomDatabase.Callback(){
@Override
public void onCreate(@NonNull SupportSQLiteDatabase db) {
super.onCreate(db);
// adding some triggers here
db.execSQL("PRAGMA recursive_triggers = 0;");
}
};
有人可以帮我解决这个问题吗?
最佳答案
改为在 RoomDatabase.Callback.onOpen()
中调用它。它需要在框架的 pragma 设置之后。您也不需要在方法覆盖中调用 super
。
关于android - 禁用 PRAGMA recursive_triggers,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53648573/