I wrote a Python script, triggers.py, to help me understand what data is available to SQLite triggers for which operations.
SQLite triggers are documented here. The key triggers are before and after for insert, update and delete. I have not explored instead of triggers yet, since those only apply to SQL views.
This section shows the output of running my triggers.py script. The triggers it uses are displayed at the bottom of the page.
create table no_pk (value text)
create table single_pk (id integer primary key, value text)
create table compound_pk (id1 integer, id2 integer, value text, primary key (id1, id2))
insert into no_pk (value) values (?) ('no_pk_value',)
before insert on no_pk:
{
"action": "insert",
"new": {
"value": "no_pk_value"
}
}
after insert on no_pk:
{
"action": "insert",
"new": {
"value": "no_pk_value"
}
}
insert into single_pk (id, value) values (?, ?) (1, 'single_pk_value')
before insert on single_pk:
{
"action": "insert",
"new": {
"id": 1,
"value": "single_pk_value"
}
}
after insert on single_pk:
{
"action": "insert",
"new": {
"id": 1,
"value": "single_pk_value"
}
}
insert or ignore:
insert or ignore into single_pk (id, value) values (?, ?) (1, 'single_pk_value_ignored')
before insert on single_pk:
{
"action": "insert",
"new": {
"id": 1,
"value": "single_pk_value_ignored"
}
}
insert or ignore into single_pk (id, value) values (?, ?) (2, 'single_pk_value_not_ignored')
before insert on single_pk:
{
"action": "insert",
"new": {
"id": 2,
"value": "single_pk_value_not_ignored"
}
}
after insert on single_pk:
{
"action": "insert",
"new": {
"id": 2,
"value": "single_pk_value_not_ignored"
}
}
insert or replace:
insert or replace into single_pk (id, value) values (?, ?) (1, 'single_pk_value')
before insert on single_pk:
{
"action": "insert",
"new": {
"id": 1,
"value": "single_pk_value"
}
}
after insert on single_pk:
{
"action": "insert",
"new": {
"id": 1,
"value": "single_pk_value"
}
}
insert or replace into single_pk (id, value) values (?, ?) (1, 'single_pk_value_replaced')
before insert on single_pk:
{
"action": "insert",
"new": {
"id": 1,
"value": "single_pk_value_replaced"
}
}
after insert on single_pk:
{
"action": "insert",
"new": {
"id": 1,
"value": "single_pk_value_replaced"
}
}
insert ... on conflict set (aka upsert):
insert into single_pk (id, value) values (?, ?) on conflict(id) do update set value=? (1, 'conflict_value', 'updated_on_conflict')
before insert on single_pk:
{
"action": "insert",
"new": {
"id": 1,
"value": "conflict_value"
}
}
before update on single_pk:
{
"action": "update",
"new": {
"id": 1,
"value": "updated_on_conflict"
},
"old": {
"id": 1,
"value": "single_pk_value_replaced"
}
}
after update on single_pk:
{
"action": "update",
"new": {
"id": 1,
"value": "updated_on_conflict"
},
"old": {
"id": 1,
"value": "single_pk_value_replaced"
}
}
insert into single_pk (id, value) values (?, ?) on conflict(id) do update set value=? (3, 'new_value', 'this_wont_be_used')
before insert on single_pk:
{
"action": "insert",
"new": {
"id": 3,
"value": "new_value"
}
}
after insert on single_pk:
{
"action": "insert",
"new": {
"id": 3,
"value": "new_value"
}
}
insert into compound_pk (id1, id2, value) values (?, ?, ?) (1, 2, 'compound_pk_value')
before insert on compound_pk:
{
"action": "insert",
"new": {
"id1": 1,
"id2": 2,
"value": "compound_pk_value"
}
}
after insert on compound_pk:
{
"action": "insert",
"new": {
"id1": 1,
"id2": 2,
"value": "compound_pk_value"
}
}
update no_pk set value = ? where rowid = ? ('no_pk_value_updated', 1)
before update on no_pk:
{
"action": "update",
"new": {
"value": "no_pk_value_updated"
},
"old": {
"value": "no_pk_value"
}
}
after update on no_pk:
{
"action": "update",
"new": {
"value": "no_pk_value_updated"
},
"old": {
"value": "no_pk_value"
}
}
update single_pk set value = ? where id = ? ('single_pk_value_updated', 1)
before update on single_pk:
{
"action": "update",
"new": {
"id": 1,
"value": "single_pk_value_updated"
},
"old": {
"id": 1,
"value": "updated_on_conflict"
}
}
after update on single_pk:
{
"action": "update",
"new": {
"id": 1,
"value": "single_pk_value_updated"
},
"old": {
"id": 1,
"value": "updated_on_conflict"
}
}
update compound_pk set value = ? where id1 = ? and id2 = ? ('compound_pk_value_updated', 1, 2)
before update on compound_pk:
{
"action": "update",
"new": {
"id1": 1,
"id2": 2,
"value": "compound_pk_value_updated"
},
"old": {
"id1": 1,
"id2": 2,
"value": "compound_pk_value"
}
}
after update on compound_pk:
{
"action": "update",
"new": {
"id1": 1,
"id2": 2,
"value": "compound_pk_value_updated"
},
"old": {
"id1": 1,
"id2": 2,
"value": "compound_pk_value"
}
}
delete from no_pk where rowid = ? (1,)
before delete on no_pk:
{
"action": "delete",
"old": {
"value": "no_pk_value_updated"
}
}
after delete on no_pk:
{
"action": "delete",
"old": {
"value": "no_pk_value_updated"
}
}
delete from single_pk where id = ? (1,)
before delete on single_pk:
{
"action": "delete",
"old": {
"id": 1,
"value": "single_pk_value_updated"
}
}
after delete on single_pk:
{
"action": "delete",
"old": {
"id": 1,
"value": "single_pk_value_updated"
}
}
delete from compound_pk where id1 = ? and id2 = ? (1, 2)
before delete on compound_pk:
{
"action": "delete",
"old": {
"id1": 1,
"id2": 2,
"value": "compound_pk_value_updated"
}
}
after delete on compound_pk:
{
"action": "delete",
"old": {
"id1": 1,
"id2": 2,
"value": "compound_pk_value_updated"
}
}
This page uses Cog to include the output of the triggers.py script. To rebuild this page after modifying the script, run this:
Created 2025-05-09T22:16:01-07:00, updated 2025-05-09T22:23:54-07:00 * History * Edit