所以我有一个mysql数据库可以使用这个触发器
delimiter $$
create trigger update_memberID
after update on memberdata
for each row
Begin
if not (new.visits <=> old.visits) then
insert into membervisit (memberID) values (new.memberID);
end if;
if not (new.memberID <=> old.memberID) then
update membervisit set memberID = new.memberID where memberID = old.memberID;
end if;
end$$
delimiter ;
现在我正在尝试在SQLite上设置相同的数据库,但是这个触发器正在运行。我对SQLite触发器的代码是
"CREATE TRIGGER IF NOT EXISTS `membership.update_memberID` " +
" AFTER UPDATE " +
" ON memberdata " +
" FOR EACH ROW BEGIN " +
" SELECT CASE WHEN (new.visits <= old.visits) THEN " +
" INSERT INTO 'membership.membervisit' ('memberID') values (new.memberID);" +
"END;\n" +
" SELECT CASE WHEN (new.memberID <= old.memberID) THEN " +
"UPDATE `membership.membervisit` SET `memberID` = `new.memberID` WHERE `memberID` =`old.memberID`;" +
"END;" +
"END;"
我收到的错误是
Exception in thread "main" java.sql.SQLException: near "INSERT": syntax error
at org.sqlite.core.NativeDB.throwex(NativeDB.java:397)
at org.sqlite.core.NativeDB._exec(Native Method)
at org.sqlite.jdbc3.JDBC3Statement.executeUpdate(JDBC3Statement.java:116)
at SQLiteSetup.main(SQLiteSetup.java:62)
C:\Users\Owner\AppData\Local\NetBeans\Cache\8.1\executor-snippets\run.xml:53: Java returned: 1
BUILD FAILED (total time: 0 seconds)
它引用的插入是
" INSERT INTO 'membership.membervisit' ('memberID') values (new.memberID);"
任何帮助将不胜感激 感谢
答案 0 :(得分:0)
CASE是一个表达;你不能用它来表达。
但在SQLite中,触发器可以有WHEN clause。 (在SQLite中,NULL安全比较是IS。)
CREATE TRIGGER update_memberID_visits
AFTER UPDATE OF visits ON memberdata
FOR EACH ROW
WHEN NEW.visits IS NOT OLD.visits
BEGIN
INSERT INTO membervisit(memberID)
VALUES (NEW.memberID);
END;
CREATE TRIGGER update_memberID_memberID
AFTER UPDATE OF memberID ON memberdata
FOR EACH ROW
WHEN NEW.memberID IS NOT OLD.memberID
BEGIN
UPDATE membervisit
SET memberID = NEW.memberID
WHERE memberID = OLD.memberID;
END;