2016-03-01 46 views
0

我使用Android的SQLite的結束SQLite觸發器語法錯誤隨時查詢

我的架構:

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; 

這是我知道一個複雜的觸發器。這個想法是從另一個表中提取價值。我不知道這是否是正確的,但是,當我替補這觸發我得到同樣的語法錯誤:

CREATE TRIGGER timecard_employee_name_delete_trigger DELETE ON timecard_fields 
WHEN OLD.timecard_field_name='employee_name' 
BEGIN 
    INSERT INTO templates (template_name) VALUES('name'); 
END; 

此錯誤總是出現在觸發器內第一個查詢:

(1 )在「;」附近:語法錯誤

INSERT INTO查詢在觸發器外正常工作。

任何想法,將不勝感激。

+0

取代'INSERT INTO模板(TEMPLATE_NAME)VALUES( '名');'你嘗試'INSERT INTO模板(ID,TEMPLATE_NAME)VALUES(NULL, '名稱') ;' – milcaepsilon

+0

剛剛嘗試過:同樣的錯誤 – NewEndian

+0

這對我有用。 –

回答

0

結果VIEWs是處理這個問題的好方法。以下是我想出了:

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