问题
I have a trigger statement like this:
CREATE TRIGGER update_customer_address UPDATE OF address ON customers
BEGIN
UPDATE orders SET address = new.address WHERE customer_name = old.name;
-- RAISE ...
END;
END;
EDIT:
I want to do a step:
int result = sqlite3_step(statement);
I know result
returns the result code like SQLITE_DONE
, SQLITE_ROW
, and SQLITE_CONSTRAINT
. I also discovered that RAISE ABORT
within TRIGGER
returns SQLITE_CONSTRAINT
to result
.
Is there a way for me to create a custom error code? I want to do something like
int result = sqlite3_step(statement);
// get custom error code here
Is it possible to raise with a custom error code in sqlite? Or are there any workarounds?
回答1:
You can throw an error using the RAISE() function. You have to use pre-defined error codes (IGNORE, ABORT, FAIL or ROLLBACK) but you can add a custom message:
CREATE TRIGGER update_customer_address UPDATE OF address ON customers
BEGIN
UPDATE orders SET address = new.address WHERE customer_name = old.name;
SELECT CASE
WHEN ( (SELECT * from orders WHERE address = 'old address!!') IS NOT NULL)
THEN RAISE (FAIL, 'Can still find old address, not everything was updated!')
END;
END;
Note that RAISE() is meant to be used only when something was wrong, see the doc for more information about raise() behavior.
回答2:
Yes ofcourse you can define custom error codes or messages. All messages sent by
raise(ERR, "message")
can be retreived in C/C++ through
const char *sqlite3_errmsg(sqlite3*);
来源:https://stackoverflow.com/questions/12701415/how-to-return-custom-error-code-in-sqlite3