1

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?

forpas
  • 160,666
  • 10
  • 38
  • 76
Estatistics
  • 874
  • 9
  • 24

1 Answers1

1

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.

forpas
  • 160,666
  • 10
  • 38
  • 76
  • I use sqlite browser. Trigger(0). I click on this, but i cannot do a thing. Where and How you place that piece of code on the sqlbrowser? Thank u for your help! :) – Estatistics Mar 11 '22 at 21:34
  • 1
    @Estatistics This is code that you can paste in any query window and execute it. – forpas Mar 11 '22 at 21:36