创建SQLITE表项的固定数量 [英] Creating a SQLITE table with a fixed number of items

查看:237
本文介绍了创建SQLITE表项的固定数量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创建一个数据库,在那里我将存储关于电池的一些统计数据。 每当电池电量下降1%,我想一个行添加到我的SQLite数据库的一些信息。

I am creating a database where I will store some statistics about the battery. Everytime the battery drops 1 percent, I want to add a row to my sqlite database with some info.

这是工作pretty的很好,但随着时间的推移,该表已成为真正的巨大的。

This is working pretty well, but with time, this table is becoming really huge.

我想要做的是设置记录的最大数(假设100为例)。

What I would like to do is to set a maximum number of records (let's say 100 for instance).

是可以设置的记录数在我的表或者我需要检查这个表,每次和code清洗吗?

Is that possible to set the number of records in my table or will I need to check this table everytime and clean it by code?

下面是我的code:

create table batterystat (_id integer primary key autoincrement, value integer not null, ....)

和我的查询

public Cursor fetchAllStats{
   return db.query(databaseName,myColumns,null,null,null,null,null);
}

感谢很多的任何帮助。

注意: 你必须要做到这一点自己和删除由code多余行可能是一个正确的答案,如果这是:-P

NOTE: "you will have to do that by yourself and delete the extra rows by code" may be a correct answer if that is the case :-p

推荐答案

我会用一个触发器和删除查询是这样的:

I would use a trigger and a delete query like this:

CREATE TRIGGER batterystat_trigger AFTER INSERT ON batterystat 
BEGIN 
    DELETE FROM batterystat where _id NOT IN (SELECT _id from batterystat ORDER BY insertion_date DESC LIMIT 100)
END

这篇关于创建SQLITE表项的固定数量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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