I didn't find a solution on this so far. Seems like there is not that much SQLite trigger stuff out there.
I'm trying to archive an update of a column if it is empty on update.
In MySQL I probably would do something like NEW.updated_at = now()
CREATE TRIGGER test_in
BEFORE UPDATE ON test
FOR EACH ROW
WHEN (NEW.updated_at IS NULL)
BEGIN
NEW.updated_at = now();
END;
But I read that you can't modify NEW
in SQLite. And the value of NEW.updated_at
in After-Trigger is already set with old value. Is there a way to temporarily store the field and use it in After-Trigger? OR prevent it from being overridden in Before-Trigger.
How could I do this in SQLite?
EDIT: My original trigger looks like this:
CREATE TRIGGER IF NOT EXISTS UpdatedAt_Update_${name}
AFTER UPDATE ON ${name}
FOR EACH ROW
WHEN OLD.updated_at = NEW.updated_at
BEGIN
UPDATE ${name}
SET updated_at = strftime('%Y-%m-%d %H:%M:%f', 'now', 'localtime')
WHERE rowid = OLD.rowid;
END;
The problem is, that if I do an update with the same updated_at
it's replaced by a new one.
EDIT 2: TL;DR
What I want to archive on update
- new value
updated_at
given: use given value - no value
updated_at
given: use current microtime
EDIT 3: So I tried as @LS_ᴅᴇᴠ suggested.
CREATE TRIGGER test_in
AFTER UPDATE OF id, content ON test
FOR EACH ROW
WHEN NEW.updated_at IS NULL
BEGIN
UPDATE test
SET updated_at = strftime('%Y-%m-%d %H:%M:%f', 'now', 'localtime')
WHERE id = NEW.id;
END
This sets updated_at
to current time when the column is empty. But only once. On next time it keeps updated_at
when no new value is given. I'm testing this with SQLite Database Browser. But it also didn't work on Android using a SQLite plugin for cordova.