简体   繁体   中英

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.

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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM