显示上周记录&使用Sqlite3数据库删除iPhone应用程序中的先前记录

时间:2013-02-11 12:32:06

标签: iphone database datetime sqlite

我想显示上周的记录&删除我的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];

获得Query ::

的价值
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";

所以,它只适用于第一,第二和第二当前日期的第3天。
它不会执行前一个月的日期,例如&#39; 30-01-2013&#39; &安培; &#39; 31-01-2013&#39;

我也尝试过这个:

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

但没有结果。:(

sqlite方面的功能是什么,因此我们可以根据这些功能执行操作,并获取上周记录的确切值。?

请帮助我解决这个问题。

提前致谢。

2 个答案:

答案 0 :(得分:0)

在SQLite中,时间戳should be stored with the year field first, 即,格式为yyyy-mm-dd hh:mm:ss

答案 1 :(得分:0)

首先,
在节省时间,

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

[currentTimeStampObj retain];

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

显示数据时&amp;删除以前的数据

//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"

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

干杯.. !!

快乐编码..