on update current_timestamp with SQLite

后端 未结 4 524
有刺的猬
有刺的猬 2020-12-04 22:26

I want to update a field with the current timestamp whenever the row is updated.

In MySQL I would do, when declaring the table

LastUpdate TIMESTAMP D         


        
4条回答
  •  自闭症患者
    2020-12-04 22:56

    John is correct about the default SQLite settings, this trigger leads to an infinite loop. To avoid recursion, use the WHEN clause.

    Following will work even if the recursive_triggers setting is on:

    PRAGMA recursive_triggers=1;     --- test
    
    CREATE TRIGGER [UpdateLastTime]
        AFTER UPDATE
        ON package
        FOR EACH ROW
        WHEN NEW.LastUpdate < OLD.LastUpdate    --- this avoid infinite loop
    BEGIN
        UPDATE Package SET LastUpdate=CURRENT_TIMESTAMP WHERE ActionId=OLD.ActionId;
    END;
    

提交回复
热议问题