我正在使用Android的SQLite
我的架构:
templateUrl: 'client/templates/tabset.html'
我正在尝试使用的触发器:
CREATE TABLE timecards (
id INTEGER PRIMARY KEY,
template_id INTEGER,
autofill_id INTEGER,
employee_name TEXT,
production_name TEXT,
week_ending TEXT,
FOREIGN KEY(template_id) REFERENCES templates(id),
FOREIGN KEY(autofill_id) REFERENCES autofills(id)
);
CREATE TABLE timecard_fields (
id INTEGER PRIMARY KEY,
timecard_id INTEGER,
timecard_field_name TEXT,
timecard_field_value TEXT,
FOREIGN KEY(timecard_id) REFERENCES timecards(id)
);
CREATE TABLE autofills (
id INTEGER PRIMARY KEY,
template_id INTEGER,
last_used TEXT DEFAULT CURRENT_TIMESTAMP
);
CREATE TABLE autofill_fields (
id INTEGER PRIMARY KEY,
autofill_id INTEGER,
autofill_field_name TEXT,
autofill_field_value TEXT,
FOREIGN KEY(autofill_id) REFERENCES autofills(id)
);
CREATE TABLE templates (
id INTEGER PRIMARY KEY,
template_name TEXT
);
我知道这是一个复杂的触发器。想法是从另一个表中提取值。我不确定它是否正确,但是,当我替换此触发器时,我得到相同的语法错误:
CREATE TRIGGER timecard_employee_name_delete_trigger DELETE ON timecard_fields
WHEN OLD.timecard_field_name='employee_name'
BEGIN
UPDATE timecards
SET employee_name = (SELECT af.autofill_field_value FROM autofills af WHERE af.autofill_id = autofill_id AND af.autofill_field_name = 'employee_name')
WHERE id=OLD.timecard_id;
END;
此错误始终出现在触发器内的第一个查询中:
(1)near“;”:语法错误
INSERT INTO查询在触发器之外工作正常。
任何想法都会受到赞赏。
答案 0 :(得分:0)
结果发现VIEW是处理此问题的更好方法。这就是我想出的:
CREATE VIEW timecard_info_view AS
SELECT DISTINCT timecards.id AS timecard_id,
templates.template_name AS template_name,
timecards.autofill_id AS autofill_id,
(SELECT CASE
WHEN ((SELECT timecard_fields.timecard_field_value FROM timecard_fields
WHERE timecard_fields.timecard_field_name='employee_name' AND timecard_fields.timecard_id=timecards.id) IS NOT NULL)
THEN
(SELECT timecard_fields.timecard_field_value FROM timecard_fields
WHERE timecard_fields.timecard_field_name='employee_name' AND timecard_fields.timecard_id=timecards.id)
ELSE
(SELECT autofill_fields.autofill_field_value FROM autofill_fields
WHERE autofill_fields.autofill_field_name='employee_name' AND autofill_fields.autofill_id=timecards.autofill_id)
END) AS employee_name,
(SELECT CASE
WHEN ((SELECT timecard_fields.timecard_field_value FROM timecard_fields
WHERE timecard_fields.timecard_field_name='picture' AND timecard_fields.timecard_id=timecards.id) IS NOT NULL)
THEN
(SELECT timecard_fields.timecard_field_value FROM timecard_fields
WHERE timecard_fields.timecard_field_name='picture' AND timecard_fields.timecard_id=timecards.id)
ELSE
(SELECT autofill_fields.autofill_field_value FROM autofill_fields
WHERE autofill_fields.autofill_field_name='picture' AND autofill_fields.autofill_id=timecards.autofill_id)
END) AS production_name,
(SELECT CASE
WHEN ((SELECT timecard_fields.timecard_field_value FROM timecard_fields
WHERE timecard_fields.timecard_field_name='week_ending' AND timecard_fields.timecard_id=timecards.id) IS NOT NULL)
THEN
(SELECT timecard_fields.timecard_field_value FROM timecard_fields
WHERE timecard_fields.timecard_field_name='week_ending' AND timecard_fields.timecard_id=timecards.id)
ELSE
(SELECT autofill_fields.autofill_field_value FROM autofill_fields
WHERE autofill_fields.autofill_field_name='week_ending' AND autofill_fields.autofill_id=timecards.autofill_id)
END) AS week_ending
FROM timecards
LEFT JOIN templates
ON timecards.template_id=templates.id
LEFT JOIN timecard_fields
ON timecard_fields.timecard_id=timecards.id
LEFT JOIN autofill_fields
ON timecards.autofill_id=autofill_fields.id