UPDATE ...
WHERE datetime =(SELECT min(datetime)FROM logtable)
$
编辑:
>显示最后100个条目 SELECT * FROM logtable
ORDER BY datetime DESC
LIMIT 100
更新:这里是一种创建130 dummyrows by
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');
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 latest activity they have done. However, naturally, I don't feel it is necessary to keep every single bit of history, so I am wondering if there is a way to configure the table to start pruning older rows once a maximum set limit is reached.
For example, if the limit is 100, and that's how many rows there currently are in the table, when another action is inserted, the oldest row is automatically removed so that there are always a maximum of 100 rows. Is there a way to configure the sqlite table to do this? Or would I have to run a cron job? I'm pretty new to sqlite so I'm sorry if this is a stupid question haha.
Thanks I appreciate any help you guys can give me.
Clarification Edit: At any given moment, I would like to display the last 100 (for example) actions/events (rows) of the table.
解决方案
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');
这篇关于限制sqlite表的最大行数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!