我想在sqlite中为RAISE(ABORT,'message')中的错误消息执行相当于此java代码的操作
String location = //initialize string
String output = String.format("Error: Insert into the SIGHTINGS table references location %s that is not found in database.", location);
这是我尝试过的,但它似乎不起作用:
CREATE TRIGGER SightingLocationError
BEFORE INSERT ON SIGHTINGS
FOR EACH ROW
WHEN NEW.LOCATION NOT IN FEATURES
BEGIN
SELECT RAISE(ABORT, 'Error: Insert into the SIGHTINGS table references location'|| CAST(NEW.LOCATION AS TEXT) ||
'that is not found in the database.');
END;
我也尝试过没有强制转换文本和整个消息部分的括号,这些方法也不起作用。 格式化错误消息的正确方法是什么,以便它显示无法插入的位置的名称,因为它不在FEATURE表中?
答案 0 :(得分:0)
看起来你正试图用触发器重新发明FOREIGN KEY
。
我建议使用:
CREATE TABLE FEATURES (ID INT PRIMARY KEY /*rest of columns*/);
CREATE TABLE SIGHTINGS(ID INT PRIMARY KEY, LOCATION INT,
FOREIGN KEY (LOCATION) REFERENCES FEATURES(id));
<强> DBFiddle Demo 强>