SQLite raise()
函数包含一个错误消息参数(请参见下面的示例)。在sqlite的“ shell”中运行SQL时,有什么方法可以显示此消息。
DROP TABLE IF EXISTS blah;
CREATE TABLE blah (
blah_id integer not null primary key,
cat_id integer not null,
worktag character(32) not null,
start_time integer not null default ( strftime('%s','now') ),
end_time not null default 0
);
CREATE TRIGGER t1 BEFORE DELETE ON blah BEGIN
SELECT RAISE(ABORT,'undeletable, simply insert new entry');
END;
CREATE TRIGGER t2 BEFORE UPDATE OF blah_id, cat_id, start_time ON blah BEGIN
SELECT RAISE(ABORT,'immutable');
END;
CREATE TRIGGER t3 BEFORE INSERT ON blah BEGIN
UPDATE blah SET end_time = strftime('%s','now')
WHERE end_time = 0 and cat_id = NEW.cat_id;
END;
INSERT INTO blah (cat_id, worktag) VALUES (1, 'The first one');
INSERT INTO blah (cat_id, worktag) VALUES (2, 'The second one');
UPDATE blah SET cat_id = 2 WHERE cat_id = 1; -- line 22
DELETE FROM blah where cat_id = 1; -- line 23
INSERT INTO blah (cat_id, worktag) VALUES (1, 'The second first one');
SELECT * from blah;
结果:
# sqlite3 -init triggers.sql
-- Loading resources from triggers.sql
Error: near line 22: constraint failed
Error: near line 23: constraint failed
1|1|The first one|1552919777|1552919777
2|2|The second one|1552919777|0
3|1|The second first one|1552919777|0