Limit an sqlite Table's Maximum Number of Rows

前端 未结 3 1471
粉色の甜心
粉色の甜心 2020-11-29 08:01

I am looking to implement a sort of \'activity log\' table where actions a user does are stored in a sqlite table and then presented to the user so that they can see the lat

3条回答
  •  难免孤独
    2020-11-29 08:49

    Another solution is to precreate 100 rows and instead of INSERT use UPDATE to update the oldest row.
    Assuming that the table has a datetime field, the query

    UPDATE ...
    WHERE datetime = (SELECT min(datetime) FROM logtable)
    

    can do the job.

    Edit: display the last 100 entries

    SELECT * FROM logtable
    ORDER BY datetime DESC
    LIMIT 100
    

    Update: here is a way to create 130 "dummy" rows by using join operation:

    CREATE TABLE logtable (time TIMESTAMP, msg TEXT);
    INSERT INTO logtable DEFAULT VALUES;
    INSERT INTO logtable DEFAULT VALUES;
    -- insert 2^7 = 128 rows
    INSERT INTO logtable SELECT NULL, NULL FROM logtable, logtable, logtable,
       logtable, logtable, logtable, logtable;
    UPDATE logtable SET time = DATETIME('now'); 
    

提交回复
热议问题