How to return custom error code in sqlite3?

£可爱£侵袭症+ 提交于 2020-02-22 17:00:33

问题


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

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!