0

我怀疑 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")

并将其以日期格式存储在数据库中,以便我可以:

  1. 在查询中利用日期比较工具。
  2. 使 FMDatabase 易于读取和写入此日期格式。

FMDatabase 是否有任何直接的方式可以与这种日期格式进行交互?或者这是 FMDatabase 的限制,我将不得不扩展 FMDatabase 类来处理读取和写回 NSDate 到 sqlite 日期格式?

谢谢

编辑: FMDatabase 的一位作者/版主不得不这样说 http: //code.google.com/p/flycode/issues/detail ?id=16#c1

4

1 回答 1

2

从以 YYYY-MM-DD 格式输入的数据库中读取日期。

我扩展了 FMResultSet 类以包含以下附加方法:

FMResultSet+Addtion.h文件:

typedef enum
{
    eText,
    eReal,
    eInteger
}EDateAndTimeDataType;
// Reference: http://www.sqlite.org/datatype3.html

typedef enum
{
    eDateString    // YYYY-MM-DD format
    // Rest is un implemented
}EDateAndTimeFunctionType;
// Refernce: http://www.sqlite.org/lang_datefunc.html

@interface FMResultSet (Additions)

-(NSDate*)dateForColumn:(NSString *)columnName withDataType:(EDateAndTimeDataType)inDateDataType functionType:(EDateAndTimeFunctionType)inFunctionType;
-(NSDate*)dateForColumnIndex:(int)columnIdx withDataType:(EDateAndTimeDataType)inDateDataType functionType:(EDateAndTimeFunctionType)inFunctionType;

@end

FMResultSet+Addition.m文件:

@implementation FMResultSet (Additions)

-(NSDate*)dateForColumn:(NSString *)columnName withDataType:(EDateAndTimeDataType)inDateDataType functionType:(EDateAndTimeFunctionType)inFunctionType
{
    return [self dateForColumnIndex:[self columnIndexForName:columnName]
                       withDataType:inDateDataType
                       functionType:inFunctionType];
}

-(NSDate*)dateForColumnIndex:(int)columnIdx withDataType:(EDateAndTimeDataType)inDateDataType functionType:(EDateAndTimeFunctionType)inFunctionType
{
    NSAssert (eText==inDateDataType, @"Only Text type is implemented for now");
    NSAssert (eDateString==inFunctionType, @"Only date() function type is implemented for now");

    NSDate *theDate = nil;
    NSString *dateString = [self stringForColumnIndex:columnIdx];
    if (nil!=dateString)
    {
        theDate = [Utility getDateFromString:dateString
                              withDateFormat:@"yyyy-MM-dd"];
        NSLog(@"Date from DB: %@", theDate);
    }
    return theDate;
}

@end

效用:

+(NSDate *)getDateFromString:(NSString *)inString withDateFormat:(NSString*)inDateFormat
{
    NSDate *date =nil;
    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
    [dateFormatter setDateStyle:NSDateFormatterMediumStyle];
    [dateFormatter setDateFormat:inDateFormat];

    NSLocale *enUSPOSIXLocale = [[NSLocale alloc] initWithLocaleIdentifier:@"en_US_POSIX"];
    [dateFormatter setLocale:enUSPOSIXLocale];
    [dateFormatter setTimeZone:[NSTimeZone timeZoneForSecondsFromGMT:0]];
    [enUSPOSIXLocale release];

    date = [dateFormatter dateFromString:inString];
    [dateFormatter release];
    return date;
}

* *编辑:虽然这适用于像“select * from table_name”这样的查询,但它在比较日期的查询中失败。为此需要解决方案。

于 2011-12-29T18:43:06.310 回答