第一次安装我的应用程序时,我以编程方式创建数据库。到目前为止工作正常。但是当我想添加一个触发器时,我得到一个异常,说IF"附近有#34;语法错误。问题是,我无法跨越源代码中的多行。那么我该怎么做呢?
private void createTriggers(SQLiteDatabase database) {
final String triggerLocationArchived = "CREATE TRIGGER location_archived AFTER UPDATE ON location FOR EACH ROW BEGIN IF NEW.deleted <> OLD.deleted THEN UPDATE sector set sector.deleted = NEW.deleted WHERE sector.location = NEW._id; END IF; END;";
database.execSQL(triggerLocationArchived);
}
有两个表:位置和扇区。每个都有一个INTEGER标志,指示该行是否已存档。因此,如果父项(位置)已存档,我希望所有子项(一个位置内的扇区)都可以存档。
答案 0 :(得分:1)
尝试此命令:
CREATE TRIGGER location_archived
AFTER UPDATE ON location
FOR EACH ROW
WHEN NEW.deleted <> OLD.deleted
BEGIN
UPDATE sector
set deleted = NEW.deleted
WHERE location = NEW._id;
END
您的代码如下所示:
private void createTriggers(SQLiteDatabase database) {
final String triggerLocationArchived = "CREATE TRIGGER location_archived AFTER UPDATE ON [location] FOR EACH ROW WHEN NEW.deleted <> OLD.deleted BEGIN UPDATE sector set deleted = NEW.deleted WHERE location = NEW._id; END";
database.execSQL(triggerLocationArchived);
}