我的SQL:
这不起作用:我希望此触发器起作用。
CREATE TRIGGER sync_VerseSearch AFTER INSERT ON `VerseTranslation`
BEGIN
UPDATE VerseSearch SET text = VerseSearch.text || ' \n ' || NEW.text WHERE VerseSearch.verse_id = NEW.verse_id;
END
此处VerseTranslation
模型具有verse_id
,并且VerseSearch
表具有与每个verse_id对应的数据。
但是当我在AFTER INSERT触发器中使用INSERT语句时,它正在工作 和AFTER UPDATE触发器中的UPDATE语句也可以工作: 示例:
CREATE TRIGGER sync_VerseSearch AFTER INSERT ON `VerseTranslation`
BEGIN
INSERT INTO VerseSearch (verse_id, text) VALUES(NEW.verse_id + 10000, NEW.text);
END
CREATE TRIGGER sync_VerseSearch AFTER UPDATE ON `VerseTranslation`
BEGIN
UPDATE VerseSearch SET text = VerseSearch.text || ' \n ' || NEW.text WHERE VerseSearch.verse_id = NEW.verse_id;
END
在插入中添加了+10000,因为在VerseSearch表中已经有数据,并且每个数据都有对应的verse_id(6236),因此它不会对主键(verse_id)造成唯一的约束错误。
问题与ACTION(INSERT触发器中的UPDATE语句)相反;
模型结构:
VerseTranslation {
@PrimaryKey(autogenerate=true)
id: Int,
verse_id: Int, // is not primary key // it is not unique, same verse_id multiple times.
source_id: Int,
text: String
}
VerseSearch {
@PrimaryKey
verse_id: Int, // here it is primary key // it's unique here
text: String // here I want to Concat(via trigger) all values(and future insert values) in VerseTranslation.text where match verse_id
}
谢谢。
答案 0 :(得分:0)
触发器必须具有唯一的名称。
您还已在插入结尾处省略了 ;
。
以下内容似乎是您想要的:-
使用:-
-- CLEAN EVRYTHING UP
DROP TRIGGER IF EXISTS sync_VerseSearch;
DROP TRIGGER IF EXISTS sync_VerseSearch_ai;
DROP TRIGGER IF EXISTS sync_VerseSearch_au;
DROp TRIGGER IF EXISTS sync_VerrseSearch_aiu;
DROP TABLE IF EXISTS VerseSearch;
DROP TABLE IF EXISTS VerseTranslation;
-- CREATE ITEMS
CREATE TABLE IF NOT EXISTS VerseSearch (verse_id INTEGER PRIMARY KEY, text TEXT);
CREATE TABLE IF NOT EXISTS VerseTranslation (verse_id INTEGER PRIMARY KEY, text TEXT);
CREATE TRIGGER sync_VerseSearch_ai AFTER INSERT ON `VerseTranslation`
BEGIN
INSERT INTO VerseSearch (verse_id, text) VALUES(NEW.verse_id + 10000, NEW.text);
UPDATE VerseSearch SET text = VerseSearch.text || ' \n ' || NEW.text WHERE VerseSearch.verse_id = NEW.verse_id;
END
;
CREATE TRIGGER sync_VerseSearch_au AFTER UPDATE ON `VerseTranslation`
BEGIN
UPDATE VerseSearch SET text = VerseSearch.text || ' \n** ' || NEW.text WHERE VerseSearch.verse_id = NEW.verse_id;
END
;
/*
CREATE TRIGGER sync_VerseSearch AFTER INSERT ON `VerseTranslation`
BEGIN
UPDATE VerseSearch SET text = VerseSearch.text || ' \n ' || NEW.text WHERE VerseSearch.verse_id = NEW.verse_id;
END
;
*/
-- TESTING THE TRIGGERS
INSERT INTO VerseSearch VALUES(null,'VSFred'),(null,'VSMary'),(null,'VSAnne'),(null,'VSJohn');
SELECT * FROM VerseTranslation;
SELECT * FROM VerseSearch;
INSERT INTO VerseTranslation VALUES(null,'Fred'),(null,'Mary'),(null,'Anne'),(null,'John');
SELECT * FROM VerseTranslation;
SELECT * FROM VerseSearch;
UPDATE VerseTranslation SET text = text||'**TEST**' WHERE NOT instr(text,'**TEST**');
SELECT * FROM VerseTranslation;
SELECT * FROM VerseSearch;
CREATE TRIGGER sync_VerseSearch_ai AFTER INSERT ON `VerseTranslation`
BEGIN
INSERT INTO VerseSearch (verse_id, text) VALUES(NEW.verse_id + 10000, NEW.text);
UPDATE VerseSearch SET text = VerseSearch.text || ' \n ' || NEW.text WHERE VerseSearch.verse_id = NEW.verse_id;
END
;
CREATE TRIGGER sync_VerseSearch_au AFTER UPDATE ON `VerseTranslation`
BEGIN
UPDATE VerseSearch SET text = VerseSearch.text || ' \n** ' || NEW.text WHERE VerseSearch.verse_id = NEW.verse_id + 10000;
END
;