表结构:
db.execSQL("CREATE TABLE TBL_REMINDERS (" //
+ "FLD_YEAR INTEGER, " //
+ "FLD_MON INTEGER, " //
+ "FLD_DATE INTEGER, " //
+ "FLD_HOUR INTEGER, " //
+ "FLD_MIN INTEGER, " //
+ "FLD_EVENT TEXT" //
+ ")" //
);
下面这个方法始终返回空结果集:
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();
}
}
}
}
经过试验,我发现,只要WHERE子句中包含表达式就会返回空结果集,例如
... WHERE 1 * FLD_NAME > 0 ...
即使表中存在满足条件 FLD_NAME 字段大于0 的行,也会返回空结果集
我的问题是:
(1)为何只要WHERE子句中包含表达式就会始终返回空结果集?
(2)或者上面的方法有其他的问题?
我也遇到多这个情况,由动态的一个值来查询结果,cursor始终为空,最后终于找到了原因。
刚开始的查询条件是这样的
Cursor alert=db.rawQuery("select LX from ALE where FZ=?", new String[]{Base64.encodeToString(sg_no.getText().toString().trim().getBytes(), Base64.DEFAULT)});
就是把一个编码后的动态值到sqlite数据库中去查询,刚开始cursor一直为null
最后改了条件
Cursor alert=db.rawQuery("select LX from ALE where FZ=?", new String[]{Base64.encodeToString(sg_no.getText().toString().trim().getBytes(), Base64.DEFAULT).trim()});
加了个trim()方法,cursor有结果了