您已经在创建一个IndexCursor
,因此不需要进行表扫描。只需使用IndexCursor#findClosestRowByEntry进行>=
搜索,然后跳过完全匹配(如果有),如下所示:
Table table = db.getTable("Members");
String dateColumnName = "DateJoined";
Column dateColumn = table.getColumn(dateColumnName);
IndexCursor cursor = CursorBuilder.createCursor(table.getIndex(dateColumnName));
String searchDateAsString = "2014/03/01";
SimpleDateFormat sdf = new SimpleDateFormat("yyyy/MM/dd");
Date search_date = sdf.parse(searchDateAsString);
cursor.findClosestRowByEntry(search_date);
if (cursor.isAfterLast()) {
System.out.println(String.format("There are no rows with %s >= %s", dateColumnName, searchDateAsString));
}
else {
// we want strictly greater than, so skip over the rows that are equal
while (search_date.equals(cursor.getCurrentRowValue(dateColumn))) {
if (!cursor.moveToNextRow()) break;
}
if (cursor.isAfterLast()) {
System.out.println(String.format("There are no rows with %s > %s", dateColumnName, searchDateAsString));
}
}
// now iterate over the remaining rows
while (!cursor.isAfterLast()) {
System.out.println(sdf.format(cursor.getCurrentRowValue(dateColumn)));
cursor.moveToNextRow();
}