开发者

android: SQLite query with between condition problem

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 = "开发者_StackOverflow社区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


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)

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜