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?
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With