查询获取与行之间certian日​​期范围(如字符串存储在DD-MM-YYYY的形式)字符串、行之、范围、形式

由网友(Memory 回忆)分享简介:我有日期存储在文本中sqllite数据库的形式。的格式是DD-MM-YYYY。我想提出一个查询,选择具有日期在给定的两个日期之间的行。当我查询数据库,查询日期属于同月,我​​得到的结果。但是,当的时间范围属于不同月份,它不返回行。对于例如:I have date stored in the form of TEXT...

我有日期存储在文本中sqllite数据库的形式。的格式是DD-MM-YYYY。我想提出一个查询,选择具有日期在给定的两个日期之间的行。当我查询数据库,查询日期属于同月,我​​得到的结果。但是,当的时间范围属于不同月份,它不返回行。对于例如:

I have date stored in the form of TEXT in sqllite database. The format is "dd-mm-yyyy". I am making a query to select rows having dates in between two given dates. When I query the database with query dates belonging to the same month, I am getting the results. But, when the date range belong to different month, it returns no rows. For eg:

当日期是类似的问题出现了。它返回行成功当日期间:2013年2月4日和2013年5月4日。有与取得的所有日期的条目行。

The problem arises when the dates are something like : 29-03-2013 to 05-04-2013. It returns rows successfully when the dates are between: 02-04-2013 and 05-04-2013. There are rows with entries made on all the dates.

以下是我已经尝试过的方式:

Following are the ways I have tried:

// MySQLiteHelper.COLUMN_DATE_DATE stores column name which is 'date'
// startdate and end date are string in form of mm-dd-yyyy
// MySQLiteHelper.TABLE_NAMES contain table name string
// MySQLiteHelper.COLUMN_MOOD_LEVEL is another column that I wish to get in return

=和> =,但没有影响到输出;

我使用BETWEEN子句和&lt已经尝试。方法如下:

I have tried using the BETWEEN clause and the <= and >=, but no effect to the output. Here is how:

// Method 1:
String selection = MySQLiteHelper.COLUMN_DATE_DATE + " BETWEEN '"+startdate+"' AND '"+ enddate+"' "; 
// Method 2:
String selection = MySQLiteHelper.COLUMN_DATE_DATE + " >= '"+startdate+"' AND "+MySQLiteHelper.COLUMN_DATE_DATE+" <= '"+ enddate+"' ";

String []colum = {MySQLiteHelper.COLUMN_MOOD_LEVEL};
Cursor cursor = database.query(MySQLiteHelper.TABLE_NAMES, colum, selection, null, null, null, null);

当我使用

cursor.getCount()

要看到的行数返回,它给了0。尽管有这些日期之间存在的条目。

to see the number of rows returned, it gives 0. Though there are entries existing in between those dates.

时的,我缺少一些基本的比较问题?我试图寻找一些previous问题,但没有成功。我真的AP preciate的帮助。

Is their some basic comparison issue that I am missing? I tried looking at some previous questions but didn't work out. I would really appreciate the help.

推荐答案

有关字符串比较正常工作,你必须使用一个格式,其中最显著场是第一位的,即 YYYY-MM-DD 。 (这也是对的SQLite的内置日期函数的唯一支持的格式。)

For string comparisons to work correctly, you must use a format where the most significant field comes first, i.e., yyyy-mm-dd. (This also is the only supported format for SQLite's built-in date functions.)

阅读全文

相关推荐

最新文章