限制会议室数据库中的行数 [英] Limit the amount of rows in a room database

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

问题描述

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

在将项目添加到数据库时,我猜它是标准查询吗?

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

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

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

从tableName中删除ID不在其中(从tableName中选择ID 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天全站免登陆