使用SQLite触发器同步脏标志

时间:2016-09-27 20:55:21

标签: android sqlite triggers

我正在尝试在两个表之间同步脏标志。表单可以有几个与之关联的Answers。如果这些答案中的任何一个都被标记为脏,我希望表单也标记为脏。以下是我目前正在尝试的,似乎无法发挥作用。

db.execSQL("CREATE TRIGGER IF NOT EXISTS markFormDirtyIfAnswerDirty AFTER UPDATE ON Answer WHEN isDirty=true " +
            "BEGIN " +
            "UPDATE Form SET dirty=true WHERE _id=OLD.formId; " +
            "END ");

这是我得到的错误:

java.lang.RuntimeException: Unable to start activity ComponentInfo{com.myapp.myappmobile.test/com.myapp.myappmobile.activities.DashboardActivity_}: java.lang.IllegalStateException: getDatabase called recursively
                                                                                at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2339)
                                                                                at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2413)
                                                                                at android.app.ActivityThread.access$800(ActivityThread.java:155)
                                                                                at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1317)
                                                                                at android.os.Handler.dispatchMessage(Handler.java:102)
                                                                                at android.os.Looper.loop(Looper.java:135)
                                                                                at android.app.ActivityThread.main(ActivityThread.java:5343)
                                                                                at java.lang.reflect.Method.invoke(Native Method)
                                                                                at java.lang.reflect.Method.invoke(Method.java:372)
                                                                                at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:905)
                                                                                at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:700)
                                                                             Caused by: java.lang.IllegalStateException: getDatabase called recursively

1 个答案:

答案 0 :(得分:0)

我找到了解决方案。事实证明,SQLite不支持true和false,而是必须使用1和0.此外,我需要在检查脏标志时使用NEW引用。正确的回答如下所示。

        db.execSQL("CREATE TRIGGER IF NOT EXISTS markFormDirtyIfAnswerDirty AFTER UPDATE ON Answer WHEN NEW.isDirty=1 " +
            "BEGIN " +
            "UPDATE Form SET dirty=1 WHERE _id=OLD.formId; " +
            "END ");