Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to return custom error code in sqlite3?

Tags:

sql

sqlite

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?

like image 818
Neigyl R. Noval Avatar asked Sep 01 '25 20:09

Neigyl R. Noval


1 Answers

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.

like image 110
mbarthelemy Avatar answered Sep 03 '25 13:09

mbarthelemy