如何从SQLite获取最近3个小时的数据 [英] How to get Last 3 hours data from SQLite
问题描述
我正在使用当前系统时间在表中插入数据,例如
System.currentTimeMillis()
.因此,在获取数据时,我仅需要获取最后3个小时的数据.
I am inserting data in table with current system time like
System.currentTimeMillis()
. So, while getting data I need to take last 3 hours data only.
下面的链接对我没有帮助.因为此链接使用的是我没有的特定日期格式.
根据最近的小时数从sqlite获取数据
Following link not help me. because this link using specific date format which I don't have.
Getting data according to last hours from sqlite
这是我的查询,但似乎不起作用.
Here Is my Query but seems it's not working.
SELECT * FROM Table1 where timestamp >= datetime('now','-3 hours')
时间戳在插入时只不过是当前系统时间.
Where timestamp is nothing but current system time while inserting.
请给我任何参考或提示.
Please give me any reference or hint.
推荐答案
您可以使用以下
Calendar cal = Calendar.getInstance();
cal.add(Calendar.HOUR, -3);
Date threeHourBack = cal.getTime();
然后您可以将threeHourBack.getTime()
传递给查询.
Then you can pass threeHourBack.getTime()
to the query.
或者您可以在SQLite中完成
Or You can do this in SQLite
SELECT * FROM Table1 where datetime(timestamp) >=datetime('now', '-3 Hour')
这篇关于如何从SQLite获取最近3个小时的数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!