Arrow
Arrow

Reputation: 163

retrive data from sqlite

What I am trying to achieve is retrieve records from SQLite database which are in the range of 3 days before current date and 3 days after current date including current date.

how to do this,please suggest some example to try out.

Upvotes: 0

Views: 156

Answers (2)

Vivek Khandelwal
Vivek Khandelwal

Reputation: 7849

First the column in which you store the date should be an long type. This column will store the milliseconds from epoch for the date.

Now for Query

    Calendar calendar = Calendar.getInstance(); // This will give you the current time.
    // Removing the timestamp from current time to point to todays date
    calendar.set(Calendar.HOUR_OF_DAY, 0);
    calendar.set(Calendar.MINUTE, 0);
    calendar.set(Calendar.SECOND, 0);
    calendar.set(Calendar.MILLISECOND, 0);
    calendar.add(Calendar.DATE, -3); // Will subtract 3 days from today.
    Date beforeThreeDays = calendar.getTime();
    calendar.add(Calendar.DATE, 6); // Will be your 3 days after today
    Date afterThreeDays = calendar.getTime();

    db.query("Table", null, "YOUR_DATE_COLUMN >= ? AND YOUR_DATE_COLUMN <= ?", new String[] { beforeThreeDays.getTime() + "", afterThreeDays.getTime() + "" }, null, null, null);

Upvotes: 1

Rahul Bisht
Rahul Bisht

Reputation: 492

Select *
From TABLE_NAME
WHERE  DATEDIFF(day, GETDATE(), COLUMN_TABLE) <= 3

Upvotes: 0

Related Questions