android:SQLite查询与条件之间的问题

发布于 2024-12-07 04:39:40 字数 3891 浏览 0 评论 0原文

我有一个具有如下架构的表:

        db.execSQL("CREATE TABLE TBL_REMINDERS (" //
                + "FLD_YEAR INTEGER, " //
                + "FLD_MON INTEGER, " //
                + "FLD_DATE INTEGER, " //
                + "FLD_HOUR INTEGER, " //
                + "FLD_MIN INTEGER, " //
                + "FLD_EVENT TEXT" //
                + ")" //

其中 FLD_MON 字段的范围为 [1,12],FLD_DATE [1,31]、FLD_HOUR [0,23]、FLD_MIN [0,59]

并且针对如下表的 rawQuery 不返回结果集:

public List<Reminder> getRemindersBetween(int y1, int m1, int d1, int h1, int min1, int y2, int m2, int d2, int h2,
        int min2) {
    String sql = "SELECT FLD_YEAR, FLD_MON, FLD_DATE, FLD_HOUR, FLD_MIN, FLD_EVENT FROM TBL_REMINDERS "//
            + "WHERE (100000000 * FLD_YEAR + 1000000 * FLD_MON + 10000 * FLD_DATE + 100 * FLD_HOUR + FLD_MIN BETWEEN ? AND ?) " //
            + "ORDER BY FLD_YEAR, FLD_MON, FLD_DATE, FLD_HOUR, FLD_MIN, FLD_EVENT ";
    SQLiteDatabase db = null;
    Cursor cur = null;
    try {
        db = new CalendarDBOpenHelper(context).getReadableDatabase();
        cur = db.rawQuery(sql, new String[] { "" + (100000000 * y1 + 1000000 * m1 + 10000 * d1 + 100 * h1 + min1),//
                "" + (100000000 * y2 + 1000000 * m2 + 10000 * d2 + 100 * h2 + min2 - 1) //
        });
        List<Reminder> list = new ArrayList<Reminder>();
        while (cur.moveToNext()) {
            int year = cur.getInt(0);
            int mon = cur.getInt(1);
            int date = cur.getInt(2);
            int hour = cur.getInt(3);
            int min = cur.getInt(4);
            String event = cur.getString(5);
            list.add(new Reminder(year, mon, date, hour, min, event));
        }

        return list;
    } finally {
        if (null != cur) {
            try {
                cur.close();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        if (null != db) {
            try {
                db.close();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }
}

而另一个 rawQuery 如下所示工作正常:

public List<Reminder> getRemindersOfDate(int y, int m, int d) {
    String sql = "SELECT FLD_HOUR, FLD_MIN, FLD_EVENT FROM TBL_REMINDERS "//
            + "WHERE FLD_YEAR=? AND FLD_MON=? AND FLD_DATE=? " //
            + "ORDER BY FLD_HOUR, FLD_MIN, FLD_EVENT";

    SQLiteDatabase db = null;
    Cursor cur = null;

    try {
        db = new CalendarDBOpenHelper(context).getReadableDatabase();
        cur = db.rawQuery(sql, new String[] { "" + y, "" + m, "" + d });

        List<Reminder> reminders = new ArrayList<Reminder>();
        while (cur.moveToNext()) {
            int hour = cur.getInt(0);
            int min = cur.getInt(1);
            String event = cur.getString(2);
            reminders.add(new Reminder(y, m, d, hour, min, event));
        }
        return reminders;
    } finally {
        if (null != cur) {
            try {
                cur.close();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        if (null != db) {
            try {
                db.close();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    } // finally
}

我猜想是“Between ... and ...”条件造成了麻烦,因为这是两个 SQL 查询之间的唯一区别。但我不确定

编辑:

事情变得有趣了。我现在确信问题是由 where 子句中的表达式引起的。在上面的第二种方法中,如果我修改

            + "WHERE FLD_YEAR=? AND FLD_MON=? AND FLD_DATE=? " //

            + "WHERE 10 * FLD_YEAR=? AND FLD_MON=? AND FLD_DATE=? " //

以及

        cur = db.rawQuery(sql, new String[] { "" + (10 * y), "" + m, "" + d });

那么我什么也得不到

I have a table with schema like this:

        db.execSQL("CREATE TABLE TBL_REMINDERS (" //
                + "FLD_YEAR INTEGER, " //
                + "FLD_MON INTEGER, " //
                + "FLD_DATE INTEGER, " //
                + "FLD_HOUR INTEGER, " //
                + "FLD_MIN INTEGER, " //
                + "FLD_EVENT TEXT" //
                + ")" //

where the FLD_MON field is in range [1,12], and FLD_DATE [1,31], FLD_HOUR [0,23], FLD_MIN [0,59]

and a rawQuery against the table as below returns no result set:

public List<Reminder> getRemindersBetween(int y1, int m1, int d1, int h1, int min1, int y2, int m2, int d2, int h2,
        int min2) {
    String sql = "SELECT FLD_YEAR, FLD_MON, FLD_DATE, FLD_HOUR, FLD_MIN, FLD_EVENT FROM TBL_REMINDERS "//
            + "WHERE (100000000 * FLD_YEAR + 1000000 * FLD_MON + 10000 * FLD_DATE + 100 * FLD_HOUR + FLD_MIN BETWEEN ? AND ?) " //
            + "ORDER BY FLD_YEAR, FLD_MON, FLD_DATE, FLD_HOUR, FLD_MIN, FLD_EVENT ";
    SQLiteDatabase db = null;
    Cursor cur = null;
    try {
        db = new CalendarDBOpenHelper(context).getReadableDatabase();
        cur = db.rawQuery(sql, new String[] { "" + (100000000 * y1 + 1000000 * m1 + 10000 * d1 + 100 * h1 + min1),//
                "" + (100000000 * y2 + 1000000 * m2 + 10000 * d2 + 100 * h2 + min2 - 1) //
        });
        List<Reminder> list = new ArrayList<Reminder>();
        while (cur.moveToNext()) {
            int year = cur.getInt(0);
            int mon = cur.getInt(1);
            int date = cur.getInt(2);
            int hour = cur.getInt(3);
            int min = cur.getInt(4);
            String event = cur.getString(5);
            list.add(new Reminder(year, mon, date, hour, min, event));
        }

        return list;
    } finally {
        if (null != cur) {
            try {
                cur.close();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        if (null != db) {
            try {
                db.close();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }
}

while another rawQuery as below works fine:

public List<Reminder> getRemindersOfDate(int y, int m, int d) {
    String sql = "SELECT FLD_HOUR, FLD_MIN, FLD_EVENT FROM TBL_REMINDERS "//
            + "WHERE FLD_YEAR=? AND FLD_MON=? AND FLD_DATE=? " //
            + "ORDER BY FLD_HOUR, FLD_MIN, FLD_EVENT";

    SQLiteDatabase db = null;
    Cursor cur = null;

    try {
        db = new CalendarDBOpenHelper(context).getReadableDatabase();
        cur = db.rawQuery(sql, new String[] { "" + y, "" + m, "" + d });

        List<Reminder> reminders = new ArrayList<Reminder>();
        while (cur.moveToNext()) {
            int hour = cur.getInt(0);
            int min = cur.getInt(1);
            String event = cur.getString(2);
            reminders.add(new Reminder(y, m, d, hour, min, event));
        }
        return reminders;
    } finally {
        if (null != cur) {
            try {
                cur.close();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        if (null != db) {
            try {
                db.close();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    } // finally
}

I guess it's the `between ... and ...' condition that is making trouble since that's the only difference between the two SQL queries. but I'm not sure

EDIT:

things are getting interesting. I'm sure now that the problem is caused by having an expression in the where clause. In the second method above, if i modify

            + "WHERE FLD_YEAR=? AND FLD_MON=? AND FLD_DATE=? " //

to

            + "WHERE 10 * FLD_YEAR=? AND FLD_MON=? AND FLD_DATE=? " //

as well as

        cur = db.rawQuery(sql, new String[] { "" + (10 * y), "" + m, "" + d });

then I get nothing

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

韶华倾负 2024-12-14 04:39:40

好吧,首先:它们绝对不一样。

第一个返回特定时间跨度内的匹配条目。
第二个返回月份、年份和日期具有给定值的条目。

编辑

尝试在表达式中使用大括号,因此(10 * FLD_YEAR)

Okay first of all: They are definitely not the same.

The first returns matching entries in a certain timespan.
The second returns entries where month, year and date have the given values.

Edit

Try to use braces for the expression, so (10 * FLD_YEAR)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文