如何获得在SQLite数据库以present日期最接近日期 [英] How to get the closest dates in sqlite database to present date
问题描述
我有保存完整的日期列的表中的SQLite数据库。我需要得到最接近的日期的present日期了该列的一切,但我不知道如何建立这个查询。
例如我有在列下列日期
2015年3月15日
2015年3月31日
2015年4月15日
2015年4月30日
2015年5月15日
2015年5月30日
如果当前日期是二零一五年四月二十零日我需要它来获取有日期的所有行。
修改
有关澄清,我需要的只是即将到来的日期,而不是过去的日期。
二○一五年四月三十〇日
此外我的日期存储为字符串。任何人都可以帮忙吗?
最终查询禄哈礼貌
光标trythis(字符串日期){
SQLiteDatabase分贝= this.getReadableDatabase();
的String [] = PARAMS新的String [] {将String.valueOf(日期)};
光标CUR = db.rawQuery(选择+ colCompID +作为_id,+ colCompClass +,+ COLNAME +,+ colPayDue +,+ colDateDue +从+ viewComps +WHERE+ colDateDue +=+(SELECT MIN(+ colDateDue +)从+ PAYMENTS +WHERE+ colDateDue +&GT =?);,则params);
返回CUR;
}
测试:
SELECT吨。* FROM表t
WHERE t.date_col =
(SELECT MIN(t2.date_col)从表t
WHERE t2.date_col> =? );
?参数将值传递是当前日期
如果date_col的数据类型是整数SQLite中,?参数将这个值传递:System.currentTimeMillis的()
注意:您可能需要删除的时间信息,如小时,分,秒,从System.currentTimeMillis的()毫秒
注意2:
如果你使用字符串作为SQLite的数据类型,你必须System.currentTimeMillis的()格式化到日期格式YYYY / MM / DD。如果使用其他格式,如M / D / YYYY - >您将有日期字符串比较的问题。请参阅下面的M / D / yyyy格式的问题:
2015年5月15日.compareTo(2015年11月30日)--->返回4是H. 0
- >意思是2015年5月15日> 2015年11月30日---错
I have an SQLite database with a table that holds a column full of dates. I need to get the date closest to the present date out of everything in that column, but I don't know how to build a query for this.
For example I have the following dates in the column
3/15/2015
3/31/2015
4/15/2015
4/30/2015
5/15/2015
5/30/2015
If the current date is 4/20/2015 I need it to get all rows that have the date.
EDIT
For clarification I need the only the upcoming dates not the past dates.
4/30/2015
Also my dates are stored as string. Can anyone help?
FINAL QUERY courtesy of Loc Ha
Cursor trythis(String Date) {
SQLiteDatabase db = this.getReadableDatabase();
String[] params = new String[]{String.valueOf(Date)};
Cursor cur = db.rawQuery("SELECT " + colCompID + " as _id," + colCompClass + "," + colName + "," + colPayDue + "," + colDateDue + " FROM " + viewComps + " WHERE " + colDateDue + "=" + "( SELECT MIN (" + colDateDue + ") FROM " + PAYMENTS + " WHERE " + colDateDue + ">=?);", params);
return cur;
}
Test this:
SELECT t.* FROM table t
WHERE t.date_col =
( SELECT MIN (t2.date_col) FROM table t2
WHERE t2.date_col >= ? );
? parameter will be passed with value is CURRENT date
IF date_col data type is Integer in SQLite, ? parameter will be passed with this value: System.currentTimeMillis()
Note: You may have to remove time info such as hour, minute, second, millisecond from System.currentTimeMillis()
Note 2: If you use String as Data type in SQLite, you have to format System.currentTimeMillis() into Date format "yyyy/MM/dd". If you use other formats such as M/d/yyyy --> You will have date String comparing issues. See issue below for M/d/yyyy format:
"5/15/2015".compareTo("11/30/2015") ---> Return 4 > 0
--> means "5/15/2015" > "11/30/2015" --- Wrong
这篇关于如何获得在SQLite数据库以present日期最接近日期的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!