简体   繁体   English

在Android中有效删除SQL行的最佳方法

[英]Best way to efficiently delete SQL rows in Android

I have a database table in my app in which I would only like to store the top ten highest numeric values. 我的应用程序中有一个数据库表,我只想在其中存储前十个最高的数值。 The columns are id(unique), name, score. 列是id(唯一),名称,得分。

What is the best method for this? 最好的方法是什么? should I manually pull all records out and delete by id? 我应该手动提取所有记录并按ID删除吗? or can I use a SQL Statement? 还是可以使用SQL语句?

Thanks, 谢谢,

Ben

Then what you could do is put a Trigger on you insert for the table. 然后,您可以做的就是在插入表的触发器上放置一个触发器 Basically the trigger would check to see if there were now more than 10 rows in the table and delete the smallest row(s). 基本上,触发器将检查表中现在是否有超过10行,并删除最小的行。 The trigger would look like this: 触发器如下所示:

CREATE TRIGGER clamp_trigger AFTER INSERT ON table_name
BEGIN
  DELETE FROM table_name
  WHERE column_name NOT IN (
  SELECT column_name 
  FROM table_name 
  ORDER BY column_name DESC
  LIMIT 10);
END;

I'm not 100% sure about SQLite, but in later versions of MySql, something like this works 我对SQLite不确定100%,但在更高版本的MySql中,类似的方法有效

DELETE FROM table_name
WHERE column_name NOT IN (
SELECT column_name 
FROM table_name 
ORDER BY column_name DESC
LIMIT 10);

Basically, select everything that isn't in the top 10 and delete it 基本上,选择前10名之外的所有内容并将其删除

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM