iOS SQLite数据库存储时间
2017-04-26 00:00
295 查看
http://www.jianshu.com/p/f78d89848f5c
当初我学fmdb操作SQLite3使用的时候遇到了一个问题,怎么存储时间呢?于是我找了很久才有了答案。今天就分享一下。
我当时想到了一个方法,使用
SQLite3存储时间是以时间戳
在我们使用fmdb创建表时类型选择
从数据库取出的时候有2种取法
要取出某段时间的数据时,只要判断时间戳就行了
注意SQLite是在MRC下运行,使用完SQLite3之后要及时关闭,不然会内存泄漏
以下是小Demo
注:相关内容我会继续更新。如果想找一些iOS方面的代码可以关注我的简书,我会持续更新,大家一起探讨探讨
在此谢谢大家阅读😊
当初我学fmdb操作SQLite3使用的时候遇到了一个问题,怎么存储时间呢?于是我找了很久才有了答案。今天就分享一下。
MesaSQLite可以查看sqlite数据库内的具体内容,小伙伴们有兴趣的可以下一个
我当时想到了一个方法,使用
FastCoder把时间转成二进制再存储,发现并不行。
SQLite3存储时间是以时间戳
NSTimeInterval的形式,意思就是要存储的时间距离1970年的秒数,是
double类型。
在我们使用fmdb创建表时类型选择
datetime,插入时间之前要把时间转成时间戳
NSTimeInterval tInterval=[myDate timeIntervalSince1970];//把时间转成时间戳
从数据库取出的时候有2种取法
1.NSTimeInterval tIn=[rs doubleForColumn:@"time"];//获取时间戳 2.NSDate* data=[rs dateForColumn:@"time"];//获取时间
要取出某段时间的数据时,只要判断时间戳就行了
注意SQLite是在MRC下运行,使用完SQLite3之后要及时关闭,不然会内存泄漏
以下是小Demo
//NULL - 空值 //INTERGER - 有符号整数类型 //REAL - 浮点数类型 //TEXT - 字符串(其编码取决于DB的编码) //BLOB - 二进制表示 @implementation ViewController - (void)viewDidLoad { [super viewDidLoad]; NSString* documentsPath=NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES).firstObject; NSString* path=[documentsPath stringByAppendingPathComponent:@"time.sqlite"]; //创建数据库 FMDatabase* db=[FMDatabase databaseWithPath:path]; //创建表 if ([db open]) { NSString* sql=@"create table if not exists timeSql(id integer PRIMARY KEY AUTOINCREMENT,time datetime)"; BOOL result=[db executeUpdate:sql]; if (result) { NSLog(@"建表成功"); }else{ NSLog(@"建表失败"); } } //存储以时间戳的形式 //添加2条时间数据 NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init]; [dateFormatter setDateFormat: @"yyyy-MM-dd HH:mm:ss"]; NSArray* timeArr=@[@"2014-02-21 16:37:05",@"2016-02-21 16:37:05"]; for (int i=0; i<2; i++) { [db executeUpdate:@"INSERT INTO timeSql(time) VALUES (?)",[dateFormatter dateFromString:timeArr[i]]]; } //查询数据 //目标:查询出比 2015-01-01 00:00:00 要晚的时间 NSDate* myDate=[dateFormatter dateFromString:@"2015-01-01 00:00:00"]; NSTimeInterval tInterval=[myDate timeIntervalSince1970];//把时间转成时间戳 NSString* sqlR=@"select time from timeSql"; FMResultSet* rs=[db executeQuery:sqlR]; while ([rs next]) { NSTimeInterval tIn=[rs doubleForColumn:@"time"];//获取查询数据中的时间戳 if (tIn>tInterval) { //比对时间戳 NSDate* data=[rs dateForColumn:@"time"]; //获取比 2015-01-01 00:00:00 要晚的时间 NSLog(@"%@",[dateFormatter stringFromDate:data]); } } //关闭数据库 if ([db open]) { [db close]; } }
注:相关内容我会继续更新。如果想找一些iOS方面的代码可以关注我的简书,我会持续更新,大家一起探讨探讨
在此谢谢大家阅读😊
相关文章推荐
- iOS SQLite数据库存储时间
- IOS中sqlite数据库利用bold类型存储与读取字典
- iOS数据存储--SQLite数据库
- iOS开发学习专题-基础知识(五) NSDate时间 NSUserDefaults本地存储 NSNotification系统通知的详细使用方式
- IOS数据存储之Sqlite数据库
- 在IOS中使用DES算法对Sqlite数据库进行内容加密存储并读取解密
- 在sql server 中执行带参数的存储过程及计算运行时间
- 存储过程参数的时间默认值解决方法
- 前段时间写的一个关于成本计算的存储过程!
- 会员开通时间处理的存储过程|软件开发
- javascript Ext JS 状态默认存储时间
- oracle 存储过程的时间计算
- 会员开通时间处理的存储过程|软件开发
- 存储过程参数的时间默认值解决方法
- 通用分页存储过程——节省你的时间,延长程序员的寿命!
- 存储过程参数的时间默认值解决方法
- 使用数据2分处理的通用分页存储过程 前半部分与后半部分数据访问时间相同,同等访问速度提高一倍
- lucene实现 某段时间内 多个存储字段 多关键字的查询 并且加入排序规则 思路
- Asp.net Membership的BUG:存储过程错误导致无法更新用户最近活动时间
- 存储里比较2007(输入)和2007-2-26(数据库)这样的时间格式