2013-01-02 166 views
0

我想實現的是從SQLite數據庫檢索記錄,這些記錄在當前日期前3天和當前日期後3天(包括當前日期)的範圍內。從sqlite中檢索數據

如何做到這一點,請建議一些例子來嘗試。

回答

1

首先在其中存儲日期欄應該是一個長型。該列將存儲日期的紀元毫秒數。

現在對於查詢

Calendar calendar = Calendar.getInstance(); // This will give you the current time. 
    // Removing the timestamp from current time to point to todays date 
    calendar.set(Calendar.HOUR_OF_DAY, 0); 
    calendar.set(Calendar.MINUTE, 0); 
    calendar.set(Calendar.SECOND, 0); 
    calendar.set(Calendar.MILLISECOND, 0); 
    calendar.add(Calendar.DATE, -3); // Will subtract 3 days from today. 
    Date beforeThreeDays = calendar.getTime(); 
    calendar.add(Calendar.DATE, 6); // Will be your 3 days after today 
    Date afterThreeDays = calendar.getTime(); 

    db.query("Table", null, "YOUR_DATE_COLUMN >= ? AND YOUR_DATE_COLUMN <= ?", new String[] { beforeThreeDays.getTime() + "", afterThreeDays.getTime() + "" }, null, null, null); 
+0

謝謝@vivek我會嘗試這一點,與將讓你知道這個 – pitambar

+0

其工作鰭片 – pitambar

0
Select * 
From TABLE_NAME 
WHERE DATEDIFF(day, GETDATE(), COLUMN_TABLE) <= 3 
+0

試試這個,讓我知道這是否解決了問題 –

+0

光標C = SDB \t \t \t \t .rawQuery( \t \t \t \t \t \t「選擇Reminder_Id,Reminder_Type, Reminder_Date,Reminder_Time,Reminder_Alert from「im試試這個爲當前日期如何實現在這個 \t \t \t \t \t \t \t \t + Reminder_Main \t \t \t \t \t \t \t \t + 「其中」 \t \t \t \t \t \t \t \t + Car_ID \t \t \t \t \t \t \t \t +「=? AND「+ Reminder_Date +」=? 」 \t \t \t \t \t \t新的String [] {summaryid,currentdate1}); – pitambar