在android中按月从数据库中获取值

时间:2015-11-11 17:40:13

标签: android sqlite

我有一个数据库,我有一个表: enter image description here

我想获得11月至11月的所有价值。 我创建了类似的东西:

public static List<Plan> getPlanListByMonth(SQLiteDatabase db, DateTime date) {

        String where = "strftime('%m', '"+"date"+"') = " +
                "'"+"11"+"'";

        Cursor cursor = db.query(TABLE_NAME, null, where,
                null, null, null, null, null);

        return getPlanList(cursor);
    }

    private static List<Plan> getPlanList(Cursor cursor) {

        List<Plan> PlanList = null;

        if (cursor != null) {

            PlanList = new ArrayList<>();

            if(cursor.moveToFirst()) {

                do {
                    Object obj = getPlan(cursor);
                    PlanList.add((Plan) obj);
                } while (cursor.moveToNext());
            }

            cursor.close();
        }

        return PlanList;
    }

但没有效果。关于如何按月获得所有价值的任何想法?

1 个答案:

答案 0 :(得分:4)

哦,好吧。

将您的查询更改为

SELECT * FROM workoutPlan WHERE strftime('%m', date) = '11'

SELECT * FROM workoutPlan WHERE CAST((strftime('%m', date)) AS INTEGER) = 11

就是这样。