Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Does SQLite store the lastmodified date of a row?

Tags:

sqlite

Does SQLite store the lastmodified date of a row, like a file system?
If not, how can I do it?

like image 582
Pentium10 Avatar asked Feb 11 '10 22:02

Pentium10


2 Answers

I think that you can only add column to your table and create trigger for updating column value with datetime('now');

like image 112
skyman Avatar answered Sep 23 '22 04:09

skyman


SQLite does not track modification or creation time on it's own. You must add this functionality yourself. To help, here's an example of a SQL trigger that can automatically set the update time on a column of your choosing.

CREATE TABLE appInfo (
    bundle_id       TEXT NOT NULL
                         PRIMARY KEY,
    appname         TEXT,
    title           TEXT DEFAULT appname,
    display_image   TEXT DEFAULT [default.gif],
    full_size_image TEXT DEFAULT [default.gif],
    bundle_version  TEXT DEFAULT [1.0],
    company_id      TEXT,
    ipaname         TEXT,
    createdatetime  TEXT DEFAULT (strftime('%Y-%m-%d %H:%M:%S:%s', 'now', 'localtime') ),
    updatedatetime  TEXT DEFAULT (strftime('%Y-%m-%d %H:%M:%S:%s', 'now', 'localtime') ) 
);

CREATE TRIGGER update_appInfo_updatetime
        BEFORE UPDATE
            ON appInfo
BEGIN
    UPDATE appinfo
       SET updatedatetime = strftime('%Y-%m-%d %H:%M:%S:%s', 'now', 'localtime') 
     WHERE bundle_id = old.bundle_id;
END;
like image 39
madduck Avatar answered Sep 19 '22 04:09

madduck