Home > Net >  in sqlite database or sqlitebrowser, in linux, how make a column write protected?
in sqlite database or sqlitebrowser, in linux, how make a column write protected?

Time:03-12

in sqlite database or sqlitebrowser, in linux, how make a column write protected?

I am opening sqlite browser. I create a table. then, some columns. eg. the column A must be write protected. But not such "option" exists as "unique key" options exists or as "not null" option.

So, i can add a bit of code to make it no other entries inserted as well no modification to this column.

How i can do that?

CodePudding user response:

There is no such thing as defining a column as write-protected in SQLite.

After you inserted all the rows that you want in the table, you can create a trigger that will not allow any more insertions and another trigger that will not allow any updates of the specific column:

CREATE TRIGGER no_more_rows BEFORE INSERT ON tablename
BEGIN
   SELECT RAISE(ABORT, 'No more rows allowed');
END;

CREATE TRIGGER no_updates BEFORE UPDATE ON tablename
BEGIN
  SELECT
    CASE
      WHEN NEW.col IS NOT old.col THEN RAISE(ABORT, 'Update of col is not allowed')
    END;
END;

Change tablename and col to the names of you table and the column.

See the demo.

  • Related