【问题标题】:Android SQLite Date query issue, cannot compare records between 2 datesAndroid SQLite 日期查询问题,无法比较两个日期之间的记录
【发布时间】:2013-01-01 01:40:10
【问题描述】:

我在本地 SQLite 数据库上运行与日期相关的查询时遇到了巨大的问题,感谢您的帮助!

我正在尝试检索其事件日期在给定年份和月份内的所有事件。为此,我计算了该月的第一个日期和最后一个日期,然后查询事件日期在此范围内的哪个位置。

我的测试数据使用: String[] testEventDates = {"2011-12-17", "2012-02-25", "2012-01-10", "2012-01-01", "2011-12-24","2012-02-25"}

但是当我在下面运行查询时,我会检索到所有这些条目,这是错误的。我的查询是 (SELECT...WHERE) "eventDate >= " + firstDay。

当我添加另一个限制时," AND "eventDate

如果您对此有任何线索,请帮助我!

// fetch events by month, between first and last days calculated
public Cursor fetchEventsByMonth(int month, int year) {
    String firstDay, lastDay, correctedMonth;

    if (month == 12) {
        lastDay = Integer.toString(year+1) + "-01-01";
    } else{
        if (month+1 < 10) {
            correctedMonth = "0" + Integer.toString(month+1);
            } else correctedMonth = Integer.toString(month+1);
        lastDay = Integer.toString(year) + "-" + correctedMonth
                + "-01";
    }
    if (month < 10) {
        correctedMonth = "0" + Integer.toString(month);
        } else correctedMonth = Integer.toString(month);
    firstDay = Integer.toString(year) + "-" + correctedMonth + "-01";
    firstDay = "2012-02-25";

    Cursor mCursor =
    mDb.query(DATABASE_TABLE_EVENTS, new String[] { KEY_EID, KEY_ENAME,
            KEY_EINTERESTS, KEY_EBENS, KEY_EDATE, KEY_ETIME, KEY_EVHO, KEY_EABBR,
            KEY_ELOCATION, KEY_ETYPE, KEY_ENATURE, KEY_ESTATUS, 
            KEY_ESIGNEDUP, KEY_ECAPACITY}, "eventDate >= " + firstDay
            // + " AND "eventDate < " + lastDay
                    , null, null, null,
            "eventDate");
    if (mCursor != null) mCursor.moveToFirst();

    return mCursor;
}   

【问题讨论】:

    标签: android sqlite date


    【解决方案1】:

    您是否尝试过直接重现查询? (我的意思是使用 SQLite Manager 等工具查询数据库)。

    注意:当您使用 STRINGS 时,您总是可以使用单引号! 我还对其进行了修改以使用选择和选择参数,因此您的安全性更安全,并且它可以自动插入单引号。试试看 所以你的查询必须是:

    Cursor mCursor =
        mDb.query(DATABASE_TABLE_EVENTS, new String[] { KEY_EID, KEY_ENAME,
                KEY_EINTERESTS, KEY_EBENS, KEY_EDATE, KEY_ETIME, KEY_EVHO, KEY_EABBR,
                KEY_ELOCATION, KEY_ETYPE, KEY_ENATURE, KEY_ESTATUS, 
                KEY_ESIGNEDUP, KEY_ECAPACITY}, "eventDate >= ? AND eventDate < ?"
                        , new String[] { firstDay, lastDay}, null, null,
                "eventDate");
        if (mCursor != null) mCursor.moveToFirst();
    

    还有一个非常重要的建议是永远不要使用字符串存储日期和时间。首先,因为它们以这种方式是静态的,相反,您必须认为人们生活在具有不同时间和时区的不同国家,因此您必须以全球的方式思考:)

    存储日期和时间的最佳方式是(在我看来)使用较长的(时间戳)。 因此,当您检索日期/时间时,您可以将其直接传递给 Date 构造函数或 Calendar 构造函数。此外,以这种方式比较日期要容易得多,而且比字符串比较要快得多:)

    【讨论】:

    • 谢谢斯特米!!现在试了一下,当月有效,其他月无效,还在摸索中
    • 奇怪的结果:对于 2012 年第 1 个月:2012-01-10(缺少 2012-01-01)||| 2012 年第 2 个月:无(2012-02-25 缺失)||| 2011 年第 12 个月:2011-12-17、2011-12-24、2011-12-25、2011-12-25、2011-12-26(这 3 个不在 DB 中,它们来自哪里?! ?)
    • 谢谢斯特米!!!它工作正常,我奇怪的结果是由于我的设备上的数据库已过时,我总是必须删除它并每次创建一个新的 AVD...:p
    • 是的,当您对 db 进行修改时,您总是要记住卸载应用程序:D 我有要遵循的检查清单,否则您将为此愚蠢的事情浪费很多时间:D
    • 是的...我真的不明白为什么不能删除数据库grrrrr
    【解决方案2】:

    您必须使用 SQLite 中的 strfttime() 函数格式化字符串日期。不同的格式在以下链接中提到。

    http://www.sqlite.org/lang_datefunc.html

    希望对你有帮助

    【讨论】:

      【解决方案3】:

      试试这个,这里我的例子是处理完整的日期,你只能处理月份和年份

      SQLite 表中归档的日期将为 TEXT,日期数据应存储为“2012-12-31 00:12:00”格式,这样比较不会给您带来麻烦,如果我们认为字段是 date_from 和 date_to 并且我们将当前日期存储在 to_date 那么我们应该得到当前日期如下和

          SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
          String to_date = sdf.format(new Date());
      

      SQL 查询应该是

      crs = db.rawQuery(
        "SELECT _id, date_from, date_to, done_or_not, list_name " +
        "FROM list_tbl " +
        "WHERE " +
        "date_from <= Datetime('"+to_date+"') AND date_to >= Datetime('"+to_date+"')", null);
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2014-02-11
        • 1970-01-01
        • 2021-10-22
        • 1970-01-01
        • 2011-08-28
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多