2011-04-28 27 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