我希望实现一种"活动日志"表,其中用户执行的操作存储在sqlite表中,然后呈现给用户,以便他们可以看到他们已完成的最新活动.但是,当然,我觉得没有必要保留每一段历史记录,所以我想知道是否有办法配置表,一旦达到最大设置限制就开始修剪旧行.
例如,如果限制为100,并且表中当前有多少行,则插入另一个操作时,将自动删除最旧的行,以便始终最多包含100行.有没有办法配置sqlite表来执行此操作?或者我必须经营一个cron工作?
澄清编辑:在任何给定时刻,我想显示表格的最后100个(例如)动作/事件(行).
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');