数据范围筛选器在Android中使用的SQLite中不起作用
我运行SQLite在两个范围内为创建的执行选择数据。要从两个日期之间选择数据,我使用以下语句数据范围筛选器在Android中使用的SQLite中不起作用,android,datetime,android-sqlite,Android,Datetime,Android Sqlite,我运行SQLite在两个范围内为创建的执行选择数据。要从两个日期之间选择数据,我使用以下语句 public ArrayList<LoopExecution> getDateRangeLoop(Date fromDate, Date toDate, int limit) { String query = "SELECT * FROM" + " loopexe " + " WHERE "
public ArrayList<LoopExecution> getDateRangeLoop(Date fromDate, Date toDate, int limit) {
String query = "SELECT * FROM"
+ " loopexe "
+ " WHERE "
+ " created_on "
+ " BETWEEN " + fromDate.getTime()
+ " AND " +toDate.getTime();
Cursor cursor = database.rawQuery(query, null);
ArrayList<LoopExecution> loopExecutions = new ArrayList<LoopExecution>();
if(cursor.moveToNext()) {
do {
LoopExecution loopExecution = new LoopExecution();
loopExecution.setId(cursor.getString(0));
Date createdOn = new Date(cursor.getLong(1));
loopExecution.setCreatedOn(createdOn);
loopExecution.setCreatedBy(cursor.getString(2));
loopExecution.setLoopId(cursor.getString(3));
loopExecution.setLoopStatus(LoopExecution.LoopStatus.valueOf(cursor.getString(4)));
loopExecution.setImage(cursor.getString(5));
loopExecution.setResultJson(cursor.getString(6));
loopExecution.setBgImage(cursor.getString(7));
loopExecution.setTrendImage(cursor.getString(8));
loopExecutions.add(loopExecution);
} while (cursor.moveToNext());
}
cursor.close();
return loopExecutions;
}
String query = "SELECT * FROM"
+ " loopexe "
+ " WHERE "
+ " created_on "
+ " BETWEEN DATE('" +fromDate.getTime() + "') "
+ " AND DATE('" + toDate.getTime() + "') ";
类型为整数
):
由于要使用长时间戳(只是一个数字),getTime()
检索该时间戳,因此必须使用
=
运算符
String query = "SELECT * FROM"
+ " loopexe "
+ " WHERE "
+ " created_on "
+ " >= " + fromDate.getTime()
+ " < " + toDate.getTime();
这只是一个硬编码日期的示例。要使其动态化,请使用:
SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
String date = formatter.format(today);
String query = "SELECT * FROM"
+ " loopexe "
+ " WHERE "
+ " created_on "
+ " BETWEEN '2017-07-10'"
+ " AND '2017-07-14'";
SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
String date = formatter.format(today);