限制房间数据库中的行数 [英] Limit the number of rows in a room database

查看:32
本文介绍了限制房间数据库中的行数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何通过删除行中最旧的项目并插入最新的项目来限制 Android 房间数据库中的行数?

How can I limit the number of rows in an Android room database by removing the oldest item in the row and inserting the newest one?

在向数据库中添加项目时,我猜这是一个标准查询?

I am guessing its a standard query when adding an item to the database?

编辑:我想限制一个数据库表的最大行数为 20.如果达到该限制,我们将删除最旧的项目并通过保留当前行插入新项目数到 20.

EDIT: I want to limit a database table to have a max row count of say 20. If that limit is reached, we remove the oldest item and insert the new one by keeping the current row count to 20.

推荐答案

我认为您可以将数据插入表中,然后删除除最后 20 行(限制)之外的所有行

I think you can insert the data into your table then remove all the rows except last 20 (limit)

要删除您可以使用以下查询

To delete you can use the following query

DELETE FROM tableName where id NOT IN (SELECT id from tableName ORDER BY id DESC LIMIT 20)

DELETE FROM tableName where id NOT IN (SELECT id from tableName ORDER BY id DESC LIMIT 20)

在这种情况下,id 是设置为自动递增的主键.如果您按日期存储它们,您也可以使用日期作为键

In this case, id is the primary key which is set to auto increment. You can use date as key as well if you are storing them by date

这篇关于限制房间数据库中的行数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆