0

我想在我的 iPhone 应用程序中显示上周记录并删除以前的记录。

所以我通过使用得到一个星期后的日期时间,

//Now
nowDate = [entryDateFormat stringFromDate:[NSDate date]];

//1 week previous
weekDate = [entryDateFormat stringFromDate:[[NSDate date] dateByAddingTimeInterval: -604800.0]];

然后,执行 SQL 查询,如

NSString *selectSQL = [NSString stringWithFormat:@"SELECT * FROM tblTest Where Time between \"%@\" and \"%@\"", weekDate, nowDate];

所以获得查询的价值::

SELECT * FROM tblTest Where Time between "04-02-2013 17:20:36" and "11-02-2013 17:20:36";

与删除以前的记录相同

Delete FROM tblTest Where Time < "04-02-2013 17:20:36";

因此,它仅适用于当前日期的第 1、第 2 和第 3 天。
它不会针对上个月的日期执行,例如“30-01-2013”​​和“31-01-2013”​​。

我也尝试过:

Delete FROM tblTest Where Time < DATE('now','-7 days');

但没有结果。:(

sqlite端的函数是什么,所以我们可以根据这些函数进行操作,得到上周记录的准确值。?

请帮助我解决这种情况。

提前致谢。

4

2 回答 2

0

在 SQLite 中,时间戳应该首先与年份字段一起存储,即格式为yyyy-mm-dd hh:mm:ss

于 2013-02-11T12:41:17.830 回答
0

首先,为了
节省时间,

NSTimeInterval currentTimeStamp = [[NSDate date] timeIntervalSince1970];
NSNumber *currentTimeStampObj = [NSNumber numberWithDouble: currentTimeStamp];

[currentTimeStampObj retain];

并将其作为新的“进入时间”字段存储到数据库中。

在显示数据和删除以前的数据时

//Date Formatter
NSDateFormatter *entryDateFormat;

//Now Date & Previous Date
NSString *nowDate, *weekDate;

NSNumber *currentNowTimeStamp, *pastWeekDateTimeStamp;

//Today
nowDate = [entryDateFormat stringFromDate:[NSDate date]];

NSTimeInterval currentTimeStamp = [[NSDate date] timeIntervalSince1970];
currentNowTimeStamp = [NSNumber numberWithDouble:currentTimeStamp];

[currentNowTimeStamp retain];

NSLog(@" Time :::: %@", currentNowTimeStamp);

//Past Week Date
weekDate = [entryDateFormat stringFromDate:[[NSDate date] dateByAddingTimeInterval: -604800.0]];

currentTimeStamp = [[[NSDate date] dateByAddingTimeInterval: -604800.0] timeIntervalSince1970];
pastWeekDateTimeStamp = [NSNumber numberWithDouble: currentTimeStamp];

[pastWeekDateTimeStamp retain];

NSLog(@" Past Week Time :::: %@", pastWeekDateTimeStamp);

询问 ::

NSString *deleteSQL = [NSString stringWithFormat:@"Delete FROM tblTest Where entry time < \"%@\"", pastWeekDateTimeStamp];

结果 ::Delete FROM tblTest Where entry time < "1360392911.010868"

NSString *selectSQL = [NSString stringWithFormat:@"SELECT * FROM tblTest Where Time between \"%@\" and \"%@\"", pastWeekDateTimeStamp, currentNowTimeStamp];

结果 ::SELECT * FROM tblTest Where entry time between "1360392911.010868" and "1360997711.010534"

我得到了我想要的确切解决方案。

干杯..!!

快乐编码..

于 2013-02-16T06:58:38.293 回答