on update current_timestamp with SQLite

后端 未结 4 532
有刺的猬
有刺的猬 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:29

    There is more efficient, nice and clean way to do it, for example:

    -- List all required fields after 'OF' except the LastUpdate field to prevent infinite loop
    CREATE TRIGGER UpdateLastTime UPDATE OF field1, field2, fieldN ON Package
    BEGIN
      UPDATE Package SET LastUpdate=CURRENT_TIMESTAMP WHERE ActionId=ActionId;
    END;
    

    The code like this one has been tested in my project. Deep sqlite trigger explanation can be found here https://www.sqlite.org/lang_createtrigger.html

提交回复
热议问题