英文:
How to empty a table in SQLITE once a limit is reached?
问题
假设我有一个名为“events”的表。我想知道如何限制表中行的数量。
例如,
当表中有30行时,我想在Android Studio的日志中打印出所有行,然后清空表格而不删除其结构。
编辑:
我知道命令**“DELETE”**。然而,我想知道是否有一种机制,只有当表中填充了30行时才调用这个DELETE函数。
英文:
lets say I have a table named events. I want to know how I can limit the number of rows in the table.
for example,
when there are 30 rows present in the table, I want to print all the rows in logging in android studio and then empty the table without deleting its structure.
EDITED :
I am aware of the command "DELETE". However, I am wondering if there is a mechanism to call this DELETE function only when lets say 30 rows have been filled in the table.
答案1
得分: 1
创建一个触发器,在每次插入新行后检查表中的行数,如果行数大于等于30,则清空表格:
CREATE TRIGGER Clear_Table
AFTER INSERT ON tablename
WHEN (SELECT COUNT(*) FROM tablename) >= 30
BEGIN
DELETE FROM tablename;
END;
英文:
Create a trigger that checks after each insertion of a new row the number of rows in the table and if this number is >= 30 the table is cleared:
CREATE TRIGGER Clear_Table
AFTER INSERT ON tablename
WHEN (SELECT COUNT(*) FROM tablename) >= 30
BEGIN
DELETE FROM tablename;
END;
答案2
得分: 0
我对sqlite还是新手。但如果我处于你的状态,我会使用以下查询创建一个空表。
DELETE FROM TARGET_TABLE
执行完这个查询后,我就可以无问题地进行插入操作。
英文:
I'm newbie on sqlite. But if I am in your status, I'll do empty table with following query.
DELETE FROM TARGET_TABLE
After executing this query, I can insert again without problem.
答案3
得分: 0
这取决于您插入表格的功能。
在您的插入方法中,您首先需要通过“count”函数检查表格中的条目数量,如果它们超过30或任何您需要的计数,运行“delete”命令。
没有针对这种要求的预定义命令。
英文:
It depends on your function that inserts in the table.
In your insert method you first need to check number of entries in your table by "count" function if they are more than 30 or whatever count you need run the "delete" command.
there is no predefined command for such requirement.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论