[英]Create trigger for SQLite, when new record is inserted, to delete old records
I have the table "log" in an SQLite database, where I write all my log files.我在 SQLite 数据库中有“日志”表,我在其中写入了所有日志文件。
However I want to pretend the database from getting to big - and the smartest way in doing this, is by using a trigger on the insert command - at least I think so...但是我想假装数据库变大 - 最聪明的方法是在插入命令上使用触发器 - 至少我是这么认为的......
When a new record is inserted, a trigger shall get fired, that deletes all records older than 10 days.当插入新记录时,将触发触发器,删除所有超过 10 天的记录。
Or...或者...
When a new record is inserted, a trigger shall get fired, that deletes all old records, which exceed a specific amount (for example 1000).当插入新记录时,将触发触发器,删除所有超过特定数量(例如 1000)的旧记录。
I need an example code.我需要一个示例代码。
Kind Regards, and thx.亲切的问候,和thx。
This will create an insert trigger that will delete anything with a create date that is more then 10 days old. 这将创建一个插入触发器,该触发器将删除创建日期超过10天的任何内容。
CREATE TRIGGER [TRIGGER_NAME] AFTER INSERT ON my_table
BEGIN
DELETE FROM LOG WHERE DATE(CREATE_DATE) > DATE('now', '-10 days');
END
If you want to do something based on size like you were saying with 1000 rows you can do something like this. 如果你想做一些基于大小的事情就像你说1000行一样,你可以做这样的事情。
CREATE TRIGGER [TRIGGER_NAME] AFTER INSERT ON my_table
BEGIN
DELETE FROM LOG WHERE ROW_NO NOT IN
(SELECT TOP 1000 ROW_NO FROM LOG ORDER BY CREATE_DATE DESC);
END
This will select the 1000 newest rows and delete anything that is not in that select statement. 这将选择1000个最新行并删除该select语句中没有的任何内容。
I have tried this approach:我试过这种方法:
CREATE TRIGGER [TRIGGER_NAME] AFTER INSERT ON my_table
BEGIN
DELETE FROM LOG WHERE ROW_NO NOT IN
(SELECT TOP 1000 ROW_NO FROM LOG ORDER BY CREATE_DATE DESC);
END
and result is that i can not create a trigger due to valid syntax error that i get结果是由于我得到的有效语法错误,我无法创建触发器
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.