My table has a column named status with data type CHARACTER. I wish to do the following.
If a row does not exists for a primary key, insert ‘active’ in
status. If a row already has an entry for a primary key, toggle
thestatus. Useative,not_activevalues for toggling.
Currently I am doing it by writing multiple queries. Is there any single query to do this like Mysql?
SQLite does not have control logic commands like
IFbecause that would not make sense in an embedded database.Implement the control logic in whatever language you’re using to access the database:
However, the toggle logic itself can be implemented with a
CASEexpression, so if you don’t care too much about clarity, you can execute the following two commands instead, in this order (theUPDATEwill do nothing if the record does not exist, and theINSERT OR IGNOREwill do nothing if the new record would violate any unique constraint):