Praveen
Praveen

Reputation: 91195

How to find the last 7 days Records in sqlite?

I would like to know the past 7 days record. I want to write a query for that in where condition. I have very basic knowledge of sqlite. Please help me with this query.

Upvotes: 1

Views: 1996

Answers (2)

progrmr
progrmr

Reputation: 77291

You need to store the date in one of the columns in your DB. In my app I convert the NSDate (iPhone obviously) to a double for storing in the DB using timeIntervalSinceReferenceDate. Given that I can query for a date range.

I wrote functions that calculate the NSDate for midnight X days in the past/future, which I use to determine the startDate and endDate of queries. Here is an excerpt from the code that will give you some idea:

char selectQuery[MAX_STR];
sprintf(selectQuery, "SELECT DateTime FROM Journal WHERE DateTime >= %f AND DateTime < %f;", 
                     [startDate timeIntervalSinceReferenceDate],
                     [endDate   timeIntervalSinceReferenceDate]);

I didn't want to convert NSDate to date time strings because I don't trust the conversions back and forth with all the various timezones and different locale's ways of formatting dates.

Upvotes: 2

Mihai Damian
Mihai Damian

Reputation: 11432

sqlite doesn't keep track of when your table rows were added. If you need this behavior you should add a timestamp column and set the current time in it when you insert a new row.

Upvotes: 1

Related Questions