sqlite数据库查询最近一小时内的数据
今天写代码需要sqlite数据库查询获取当前时间内一个小时内的数据,经过查询了几个网上教程发现理论上可行的sql如下
NSString *querySql = [NSString stringWithFormat:@"select * from %@ where %@='0' and datetime(%@)>datetime('now','-%d minutes') and %@ = '0' and %@ <> '0' order by %@ desc", kTableName_PrintRecord, kDatabase_Fields_deleted, kDatabase_Fields_createDateTime, hour * 60,kDatabase_Fields_PrintRecord_printType,kDatabase_Fields_PrintRecord_printStatus,kDatabase_Fields_createDateTime];
DEBUG_LOG(querySql);
FMResultSet *rs = [_db executeQuery:querySql];
但是利用以下sql语句却是再实际的操作中是过滤不到的,无奈只有通过计算的方式来比较。上面的sql暂且现记录下来方便日后再整理寻找到真正的可用sql,通过的oc的计算代码如下
NSDate *createDateTime = entity.createDateTime;
//结束时间
NSDate *currentDateTime = [NSDate date];
//得到相差秒数
NSTimeInterval timeInterval = [currentDateTime timeIntervalSinceDate:createDateTime];
int hours = timeInterval / 3600;
if (hours < hour)
{
[array addObject:entity];
}