排序日期SQLite数据库? [英] Sorting dates in sqlite database?

查看:195
本文介绍了排序日期SQLite数据库?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经开发了一个应用程序..For,我有一个数据库,它有很多表。其中一个表有日期列。 我的问题是我要的日期排序,所以必须选择最老的例如 OCT-24-2012 从下表中的列

I have developed an App ..For that I have a database and it has many tables. One of the table has date column. My question is I have to sort the dates and have to pick oldest one Like Oct-24-2012 from below table column

Col​​umn_Date
------------

Column_Date
------------

NOV-07-​​2012
NOV-21-2012
NOV-25-2012
OCT-25-2012
OCT-24-2102

Nov-07-2012
Nov-21-2012
Nov-25-2012
Oct-25-2012
Oct-24-2102

我应该怎么做的....我已经存储了日期列串......如果我需要改变的数据类型为日期列,让我知道,数据类型,我应该使用哪个。我应该如何排序Column.According到的数据类型。

How should I do that....I have stored the date column in string... If I need to change the datatype for date Column, Let me know which Datatype should i used. And How Should I sort the Column.According to That datatype..

推荐答案

在SQLite不真的有一个日期类型。您可以存储日期为使用他们的predefined一种格式字符串见 http://www.sqlite.org/lang_datefunc.html 了解更多信息。

In sqlite does not really have a date type. You can store dates as strings using one of their predefined formats see http://www.sqlite.org/lang_datefunc.html for more information.

一个时间字符串可以在以下任何一种格式:

A time string can be in any of the following formats:

YYYY-MM-DD
YYYY-MM-DD HH:MM
YYYY-MM-DD HH:MM:SS
YYYY-MM-DD HH:MM:SS.SSS
YYYY-MM-DDTHH:MM
YYYY-MM-DDTHH:MM:SS
YYYY-MM-DDTHH:MM:SS.SSS
HH:MM
HH:MM:SS
HH:MM:SS.SSS
now
DDDDDDDDDD

您需要将它们存储在YYYY-MM-DD,那么你可以为了通过ASC限制1对它们进行排序,以获得最早日期。 因此,而不是

You need to store them in YYYY-MM-DD then you can sort them order by asc limit 1 to get the oldest date. So instead of

Column_Date
------------
Nov-07-2012
Nov-21-2012
Nov-25-2012
Oct-25-2012
Oct-24-2102

您必须保存它们而不是像这样

You will have to store them like this instead

Column_Date
------------
2012-11-07
2012-11-21
2012-11-25
2012-10-25
2012-10-24

最后,你看行,如果任何

Finally you read the rows if any

Cursor oldestDateCursor = db.query("DateTableName", null, null, null, null, null, "date_column ASC LIMIT 1");
if (oldestDateCursor.moveToFirst())
{
    String date = oldestDateCursor.getColumnName(oldestDateCursor.getColumnIndex("date_column"));
}
oldestDateCursor.close();

这篇关于排序日期SQLite数据库?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆