如何在JS包装器中编写sqlite触发器 [英] how to write a sqlite trigger in JS wrapper
问题描述
让我们假设2个表:
table1 [idTble1,elmt1_T1,elmt2_T1]
table2 [idTble2,id_Tble1,elmt1_T2,index]
我想创建一个在<$ c之后运行的触发器$ c>在表1上插入,然后在表2中插入新行,其中
Let's assume 2 tables:
table1[idTble1, elmt1_T1, elmt2_T1]
table2[idTble2, id_Tble1, elmt1_T2, index]
I want to create a trigger that runs after Insert On table1
, and Insert a new row in Table2 where
id_Tble1 = new.idTble1
elmt1_T2 = new.elmt1_T1
,索引是用户设置的值,仅保存在表2中。
我已经尝试过以下操作
and index is a value set by the user and that is only saved in Table2. I have tried the following
var createTrigger = "CREATE TRIGGER triggerInsert AFTER INSERT ON Table1 REFERENCING NEW AS NEW OLD AS OLD FOR EACH ROW BEGIN INSERT INTO Table2 (id_Tble1, elmt1_T2, index) VALUES (:new.idTble1, :new.elmt_T1, ?); END;";
db.transaction(function(tx) {
tx.executeSql(createTrigger, [i], null, onError);
});
但是我遇到了一个错误(无法准备语句( REFERENCING附近的1:语法错误)。
but i got an error (could not prepare statement (1 near "REFERENCING": syntax error).
推荐答案
几个小时后,我至少可以部分工作了,我必须承认软件SQLiteManager,即使在演示版本,有助于调试代码。
这是有效的触发器:
After several looooong hours, I got it working, at least partially. I have to admit that the software SQLiteManager, even in demo version, was helpful in debugging the code. Here is the trigger that works:
var createTrigger = "CREATE TRIGGER triggerInsert AFTER INSERT ON Table1 BEGIN INSERT INTO Table2 (id_Tble1, elmt1_T2, index) VALUES (new.idTble1, new.elmt_T1, new.index); END";
db.transaction(function(tx) {
tx.executeSql(createTrigger, [], null, onError);
});
我只说一句是因为我无法让触发器使用表单中的索引值,所以我要做的就是在表1中创建一个新字段(索引)。
When I say partially, is because I have not been able to have the trigger use the value of 'index' in the form. So what I had to do was to create a new field (index) in table1.
这篇关于如何在JS包装器中编写sqlite触发器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!