【问题标题】:Get values from database by month in android在android中按月从数据库中获取值
【发布时间】:2015-11-11 17:40:13
【问题描述】:

我有一个数据库,其中有一个表:

我想获取 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;
    }

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

【问题讨论】:

  • 我复制粘贴你的字符串。这是在调试器中查看查询的方式: SQLiteQuery: SELECT * FROM trainingPlan WHERE strftime('%m', '2015-11-30') = 11
  • 这是实际查询:SQLiteDirectCursorDriver: SELECT * FROM exercisePlan WHERE strftime('%m', date) = 11 且无效
  • 是的!最后!十分感谢!你是我的英雄:D
  • 有时最容易掉入的陷阱就在我们的眼皮底下,嗯?

标签: android sqlite


【解决方案1】:

哦,好吧。

将您的查询更改为阅读

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

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

仅此而已。

【讨论】:

    猜你喜欢
    • 2016-12-19
    • 1970-01-01
    • 1970-01-01
    • 2017-11-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多