2016-12-23 69 views
-1

假設你有一個表有以下的列(所有整數)SQLite的查詢,以獲得未來30天內行,​​(從現在到現在+30天)

taskId | day | month | year 
------------------------ 
1  | 4 | 4  | 2016 
1  | 3 | 5  | 2016 
1  | 12 | 5  | 2016 
1  | 30 | 5  | 2016 

假定日期現在是4/4/2016 如何在Sqlite查詢中獲取現在和未來30天之間的記錄。 意味着未來30天的任務。

所以結果應該是,

1  | 4 | 4  | 2016 << today is 4/4/2016 
1  | 3 | 5  | 2016 

感謝

回答

2

要將這些列轉換爲SQLite的默認日期格式,請將u SE printf()

printf("%04d-%02d-%02d", year, month, day) 

然後,您可以使用內置日期函數:

SELECT * 
FROM MyTable 
WHERE printf(...) BETWEEN date('now') AND date('now', '+30 days'); 
1

首先

我不得不說,與date工作本來waaaay更好,或者也與date.getTime工作這返回long

據說

從Java端,你將不得不做這樣的事情:

Caendar c = Calendar.getIstance(); 

int currentDay = c.get(Calendar.DAY_OF_MONTH); 
int currentMonth = c.get(Calendar.MONTH) + 1; //it's 0 based 
int currentYear = c.get(Calendar.YEAR); 

c.add(Calendar.DAY_OF_MONTH, 30); 
int nextMonthDay = c.get(Calendar.DAY_OF_MONTH); 
int nextMonthMonth = c.get(Calendar.MONTH) + 1; //it's 0 based 
int nextMonthYear = c.get(Calendar.YEAR); 

String myQuery = String.Format("select * from myTable where (day >= {0} and month >= {1} and year >= {2}) and (day < {3} and month <= {4} and year <= {5})", currentDay, currentMonth, currentYear, nextMonthDay, nextMonthMonth, nextMonthYear); 

這將需要所有行日期> =當前和日期是< =下月

希望這會有所幫助

+0

這是我要遵循的最佳途徑。謝謝 – Hatim