SQLite Forum

get changed columns data (previous/ after) on update row and previous data before delete row
Login
Triggers meanwhile are good if you want to do basic SQL with the changed values, like insert them into an auditing table. You can set up an on update trigger on each field to insert into an auditing table the field name, change time, old and new values, etc. Or an on delete trigger to insert all the old fields into a delete table along with the time.

With triggers it's all going to be handled by the SQLite engine in the background and none of the values or info will be sent back to the controlling program. It will basically be "well, you left me instructions on what to do on a delete, so I did it. No need to bug you every time about it."


--Not necessarily the best way to do it, but an example.

create table table1 (a text, b int, c float);

create table table1_audit
(change_time, change_type, old_a, old_b, old_c, new_a, new_b, new_c);

create trigger trg_table1_deletes
before delete on table1
begin
insert into table1_audit (change_time, change_type, old_a, old_b, old_c)
values (datetime('now'), 'delete', old.a, old.b, old.c);
end;

create trigger trg_table1_changes --only the fields that changed
before update on table1
begin
insert into table1_audit
values (datetime('now'), 'update',
case when new.a is not old.a then old.a end,
case when new.b is not old.b then old.b end,
case when new.c is not old.c then old.c end,
case when new.a is not old.a then new.a end,
case when new.b is not old.b then new.b end,
case when new.c is not old.c then new.c end);
end;


sqlite> insert into table1 values ('one', 1, 1.0), ('two', 2, 2.0);

sqlite> delete from table1 where b = 1;

sqlite> update table1 set c = 2.1 where b = 2;

sqlite> update table1 set a = 'three', b = 3, c = 3.0 where b = 2;

sqlite> select * from table1;
a|b|c
three|3|3.0

sqlite> select * from table1_audit;
change_time|change_type|old_a|old_b|old_c|new_a|new_b|new_c
2020-09-16 14:31:10|delete|one|1|1.0|||
2020-09-16 14:31:20|update|||2.0|||2.1
2020-09-16 14:31:37|update|two|2|2.1|three|3|3.0

sqlite>