//根据时间删除
- (void)deleteDataWithTime:(NSString *)time at:(NSString *)tableName{
//NSString *deleteSql = @"delete from LBBMesTable where Id =?";
NSString *tab = [tableName stringByReplacingOccurrencesOfString:@".db" withString:@""];
NSString *deleteSql = [NSString stringWithFormat:@"delete from %@ where time <?",tab];
// NSData *messageData = [message dataUsingEncoding:NSUTF8StringEncoding];
NSString *path = [NSHomeDirectory() stringByAppendingString:[NSString stringWithFormat:@"/Documents/%@",tableName]];
_dataBase = [FMDatabase databaseWithPath:path];
if([_dataBase open]){
if (![_dataBase executeUpdate:deleteSql,time]) {
NSLog(@"delete error:%@",_dataBase.lastErrorMessage);
}
}
[_dataBase close];
}
这样可以直接删除哪个时间之前的数据,其中time是以字符串存储的,相当于比较字符串了,
//计算距离现在多少秒的时间,返回的是时间的完整字符串,如2014-01-04 06:06:06,如果传入的是负数,则是以前的时间
+(NSString *)timeStrSinceNow: (NSTimeInterval)timeInterval{
NSDate* dat = [NSDate dateWithTimeIntervalSinceNow:timeInterval];//计算出这么多时间之前的一个时间
NSDateFormatter *formatter=[[NSDateFormatter alloc] init];
[formatter setDateFormat:@"yyyy-MM-dd HH:mm:ss"];
NSString *pastTime = [formatter stringFromDate:dat];
return pastTime;
}