4

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?

2 Answers 2

5

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.

Sign up to request clarification or add additional context in comments.

Comments

-1

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*);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.