0

現在の日付から 3 日前後の Parsed_Reminder_Date と比較した後、すべてのレコードを取得したいのですが、データベースで Parsed_Reminder_Date は日付を保存するために使用される列であり、そのデータ型は長いです。

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();

        long threedaybefore=beforeThreeDays.getTime() ;
        long threedayafter=afterThreeDays.getTime() ;

        Log.i("beforeThreeDays",Long.toString(threedaybefore));
        Log.i("afterThreeDays",Long.toString(threedayafter));


        String Reminder_Main = "Reminder_Main",ID = "ID", Reminder_Date = "Reminder_Date", Parsed_Reminder_Date = "Parsed_Reminder_Date";
        Cursor remindercursor = sdb
                .rawQuery(
                        "select Reminder_Id,Reminder_Type,Reminder_Time,Reminder_Date,Reminder_Alert,Reminder_Done,Reminder_byodometer,Reminder_bydate from "
                                + Reminder_Main
                                + " where "                             
                                + ID 
                                + " = ? AND "
                                + Parsed_Reminder_Date
                                + " >= ? OR  " 
                                + Parsed_Reminder_Date 
                                + " <= ? ",
                        new String[] {summaryid, Long.toString(threedaybefore), Long.toString(threedayafter)});

        remindercursor.moveToFirst();

これを実行すると、不要なレコードがすべて取得されます。どこが間違っているかを修正してください。

4

1 に答える 1

0

このような単純なクエリを使用します

String query = "select Reminder_Id,Reminder_Type,Reminder_Time,Reminder_Date,Reminder_Alert,Reminder_Done,Reminder_byodometer,Reminder_bydate from "
                            + Reminder_Main
                            + " where "                             
                            + ID 
                            + " = "+ summaryid +" AND "
                            + Parsed_Reminder_Date
                            + " >= "+ threedaybefore +" AND  " 
                            + Parsed_Reminder_Date 
                            + " <= " + threedayafter;
Cursor remindercursor = sdb.rawQuery(query, null);

または他のトリックを使用します

1)すべてのデータをフェッチするクエリを記述します。

String query = "select Reminder_Id,Reminder_Type,Reminder_Time,Reminder_Date,Reminder_Alert,Reminder_Done,Reminder_byodometer,Reminder_bydate from "
                            + Reminder_Main
                            + " where "                             
                            + ID 
                            + " = "+ summaryid;
Cursor remindercursor = sdb.rawQuery(query, null);
Date d = null
if (cursor.getCount() > 0) {
        if (cursor.moveToFirst()) {
            do {
                d = (<use Date Parser Here as per your dateFormat>cursor.getString(<Index>));
                if(d.after(beforeThreeDays) && d.before(afterThreeDays)){
                        // go Ahead.....
                } 
            } while (cursor.moveToNext());
        }
    }
    cursor.close();
于 2013-02-12T09:05:25.807 に答える