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

时间:2011-09-28 16:31:39

标签: android sqlite between

我有一个包含这样的架构的表:

        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
}

我猜这是“之间......和......之间”的条件,因为这是两个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 });

然后我什么都没得到

1 个答案:

答案 0 :(得分:0)

好的首先:他们肯定不一样。

第一个返回特定时间段内的匹配条目 第二个返回月,年和日具有给定值的条目。

修改

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