简体   繁体   中英

FMDB how can I query data from date range on Sqlite iPhone dev

I am trying to perform a query on a table where there is a date field and I want to query over a range of values The where clause looks like the following:

[db executeQuery:@"select * from test where myDate BETWEEN date('now','-7 days') and date('now')"];

But it doesn't seem to work :(

If you used DATETIME column with table declaration, you can do this:

[db executeQuery:@"SELECT * FROM test WHERE myDate BETWEEN ? and ?", [[NSDate dateWithTimeInterval:-86400*7 sinceDate:[NSDate date]], [NSDate date]];

Note that -86400 is seconds of a day, negative indicates "days before" like your question.

Also note that BETWEEN syntax includes the very second of the date you set. Use WHERE myDate > ? and myDate < ? WHERE myDate > ? and myDate < ? if you need non-inclusive match.

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