Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get Last 3 hours data from SQLite

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.

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.

like image 560
Sandip Armal Patil Avatar asked Oct 19 '25 17:10

Sandip Armal Patil


1 Answers

You can get TimeStamp of Time 3 Hours back using following

Calendar cal = Calendar.getInstance();
cal.add(Calendar.HOUR, -3);
Date threeHourBack = cal.getTime();

Then you can pass threeHourBack.getTime() to the query.

Or You can do this in SQLite

SELECT * FROM Table1 where datetime(timestamp) >=datetime('now', '-3 Hour')
like image 50
Shashank Kumar Avatar answered Oct 22 '25 08:10

Shashank Kumar



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!