Delete based on time
-(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];
}
This allows the data to be deleted directly, where time is stored as a string, equivalent to the comparison string,
Calculate the distance now how many seconds, the return is the full string of time, such as 2014-01-04 06:06:06, if the incoming negative, it is the previous time
+ (NSString *) Timestrsincenow: (nstimeinterval) timeinterval{
nsdate* dat = [NSDate datewithtimeintervalsincenow:timeinterval];//calculates a time before so much time
NSDateFormatter *formatter=[[nsdateformatter alloc] init];
[Formatter setdateformat:@ "Yyyy-mm-dd HH:mm:ss"];
NSString *pasttime = [Formatter stringfromdate:dat];
return pasttime;
}
Delete data from sqlite3 based on time