在Sqlite数据库中对日期进行排序(Sort the date in Sqlite database)
我正在制作一个iPhone应用程序,我将日期和时间存储在一个列中,现在我想按照先发生的顺序对日期和时间进行排序。
我怎样才能做到这一点? 请给我一些解决方案。
I am making an iPhone application in which I am storing date and time in a column now I want to sort the date and time in the order which occurs first.
How can I do this? please give me some solutions.
最满意答案
将NSDate另存为NSTimeInterval :
NSTimeInterval timeInterval = [[NSDate date] timeIntervalSince1970];NSTimeInterval只是一个可以保存在SQLite数据库中的double NSTimeInterval数。
使用简单的SQL查询,您可以选择排序:
SELECT * FROM dateTable ORDER BY myDateColumn DESC并且让NSDate回归:
NSDate *date = [NSDate dateWithTimeIntervalSince1970:databaseTimeInterval];Save the NSDate as NSTimeInterval:
NSTimeInterval timeInterval = [[NSDate date] timeIntervalSince1970];The NSTimeInterval is just a double which you can save in the a SQLite database.
With an simple SQL query you can then select the sort then:
SELECT * FROM dateTable ORDER BY myDateColumn DESCAnd to get the NSDate back just:
NSDate *date = [NSDate dateWithTimeIntervalSince1970:databaseTimeInterval];更多推荐
发布评论