通过FMDatabase读写NSDate到sqlite数据库
我怀疑 NSDate 是否可以使用 FMDatabase 直接存储或从数据库中读取。
根据我的研究,FMDatabase 从记录中读取日期,期望该元组为双精度值。 FMDatabase 在此方法中将该双精度值转换为日期:
- (NSDate*) dateForColumn:(NSString*)columnName; {
int columnIdx = [self columnIndexForName:columnName];
if (columnIdx == -1) {
return nil;
}
return [NSDate dateWithTimeIntervalSince1970:[self doubleForColumn:columnName]];
}
这种方法的主要问题是,对于在数据库中手动输入数据的人来说,很难让代码中的 FMDatabase 能够理解这些数据。所以有人不能简单地在 GUI 中输入双值日期。一种方法是使用这些函数之一
date(timestring, modifier, modifier, ...)
time(timestring, modifier, modifier, ...)
datetime(timestring, modifier, modifier, ...)
julianday(timestring, modifier, modifier, ...)
strftime(format, timestring, modifier, modifier, ...)
,但这里的开销又是每次运行查询使用与此类似的插入操作输入记录:
Compute the current date.
SELECT date('now');
Compute the last day of the current month.
SELECT date('now','start of month','+1 month','-1 day');
我希望将日期以 以下格式存储在数据库中:
TEXT as ISO8601 strings ("YYYY-MM-DD")
并将其存储为数据库中的日期格式,以便我可以:
- 在查询中利用日期比较工具。
- 使 FMDatabase 易于读取和写入此日期格式。
FMDatabase 有没有直接的方法可以与这种日期格式交互?或者这是 FMDatabase 的限制,我必须扩展 FMDatabase 类来处理 NSDate 的读写到 sqlite 日期格式?
谢谢
编辑:FMDatabase 的作者/版主之一必须说关于它:http://code.google.com/p/flycode/issues/detail?id=16#c1
I have a doubt whether NSDate can be directly stored or read from the database using FMDatabase or not.
As per my research, FMDatabase reads date from record expecting that tuple to be a double value. FMDatabase converts that double value to date in this method:
- (NSDate*) dateForColumn:(NSString*)columnName; {
int columnIdx = [self columnIndexForName:columnName];
if (columnIdx == -1) {
return nil;
}
return [NSDate dateWithTimeIntervalSince1970:[self doubleForColumn:columnName]];
}
The main problem with this approach is, it would be difficult for the person who is manually entering data in the database so that it would be understandable by FMDatabase in the code. So someone cannot simply enter the date in double value in GUI. One approach is to use one of these functions
date(timestring, modifier, modifier, ...)
time(timestring, modifier, modifier, ...)
datetime(timestring, modifier, modifier, ...)
julianday(timestring, modifier, modifier, ...)
strftime(format, timestring, modifier, modifier, ...)
But the overhead here is again to run a query each time for entering a record using insert operation similar to this:
Compute the current date.
SELECT date('now');
Compute the last day of the current month.
SELECT date('now','start of month','+1 month','-1 day');
I would like the date to be stored in the database in following format:
TEXT as ISO8601 strings ("YYYY-MM-DD")
And store it as Date format in the database so that I can:
- Leverage the date comparison facility in queries.
- Make FMDatabase understandable to read and write this date format.
Is there any direct way where FMDatabase can interact with this kind of date format? Or is this a limitation of FMDatabase and I will have to extend the FMDatabase class to handle reading and writing back of NSDate to the sqlite date format?
Thanks
Edit: One of the author / moderator of FMDatabase has to say this about it: http://code.google.com/p/flycode/issues/detail?id=16#c1
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
从数据库中读取日期,以 YYYY-MM-DD 格式输入。
我扩展了 FMResultSet 类以包含以下附加方法:
FMResultSet+Addtion.h 文件:
FMResultSet+Addition.m 文件:
实用程序:
**编辑:虽然这适用于“select * from table_name”之类的查询,但在比较日期的查询中却失败了。需要解决这个问题。
Reading date from database which is entered in the format YYYY-MM-DD.
I have extended the FMResultSet class to include the following additional methods:
FMResultSet+Addtion.h file:
FMResultSet+Addition.m file:
Utility:
**Edit: Though this works for queries like "select * from table_name" it fails in queries comparing for dates. Need solutions for this.