2011-04-28 55 views
0

什么是存储日期的最佳格式,以便稍后使用Objective-C dateformatter轻松格式化?日期将以unix时间戳开始,然后变成最容易处理的格式,存储在SQLite3数据库中,然后通过dateformatter检索和处理。我可以只使用时间戳吗?或者将它转换为YYYY/mm/dd或更好的方法。iPhone SQLite3日期时间问题

回答

2

我想你会希望以YYYYMMDDHHMMSS格式存储它,以方便排序。

你是直接写sqlite代码吗?为什么不使用像Core Data这样的托管数据框架(底层数据存储是sqlite),会让你的生活变得更轻松。这个小例子使用NSSortDescriptor通过FileDate列进行排序。 AssetItem是NSManagedObject子类

NSFetchRequest *nsrequest = nil; 
NSEntityDescription *entity = nil; 

NSError *error; 

// check to see if the scene exists (could have been downloaded previously) 
nsrequest = [[NSFetchRequest alloc] init]; 
entity = [NSEntityDescription entityForName:@"AssetItem" inManagedObjectContext:managedObjectContext];   
[nsrequest setSortDescriptors:[NSArray arrayWithObject:[[[NSSortDescriptor alloc] initWithKey:@"FileDate" ascending:NO] autorelease]]];  
[nsrequest setEntity:entity]; 

NSArray *objs = [[managedObjectContext executeFetchRequest:nsrequest error:&error] retain]; 

for (AssetItem *item in objs) { 
    NSLog(@"file date is: %@", [item FileDate]); 
} 
[objs release]; 
[nsrequest release] 

;

您可以使用内置的Xcode数据建模工具进行很多建模。

+0

我从主MySQL数据库中获取数据,将其修剪成小型的SQLite数据库,在必要时通过下载实现,然后使用FMDB与SQLite数据库一起工作。我再也不会在Obj-C中使用本机SQLite调用。 – Josh 2011-04-28 16:36:28

+0

你的数据模型会改变吗?或只是数据?如果后者你可以在Core Data中建立你的sqlite数据库的模型,然后从iPhone中得到空的.sqlite数据库文件,并执行数据服务器端的翻译并下载翻译后的.sqlite数据库。 – 2011-04-28 16:54:37