zoukankan      html  css  js  c++  java
  • 根据时间删除sqlite3中数据

    //根据时间删除

    - (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;

    }

  • 相关阅读:
    对文件的操作
    三级菜单优化
    三级菜单项目
    对字符串的操作(一些重要的方法)
    四、saltstack如何管理对象?
    三、saltstack证书管理
    二、saltstack基础配置
    一、saltstack简介和安装
    set集合
    异常处理语法
  • 原文地址:https://www.cnblogs.com/ios-wyh/p/4198707.html
Copyright © 2011-2022 走看看