我的表包含数据以及日期创建。我想根据两个给定的日期范围或一个特定月份从表格中选择数据。
我不知道该怎么做,因为在列上创建的是text类型并存储为dd-MM-yyyy HH:mm:ss
我正在使用rawQuery()来获取数据。
答案 0 :(得分:15)
您可以这样做:
mDb.query(MY_TABLE, null, DATE_COL + " BETWEEN ? AND ?", new String[] {
minDate + " 00:00:00", maxDate + " 23:59:59" }, null, null, null, null);
minDate
和maxDate
(日期字符串)构成您的范围。此查询将获取MY_TABLE
中属于此范围的所有行。
答案 1 :(得分:0)
这是一些有用的
//Get Trips Between dates
public List<ModelGps> gelAllTripsBetweenGivenDates(String dateOne, String dateTwo) {
List<ModelGps> gpses = new ArrayList<>();
SQLiteDatabase database = dbHelper.getReadableDatabase();
final String columNames[] = {DBHelper.COLUMN_ID, DBHelper.COLUMN_NAME, DBHelper.COLUMN_LATITUTE, DBHelper.COLUMN_LONGITUDE, DBHelper.COLUMN_ALTITUDE, DBHelper.COLUMN_DATE, DBHelper.COLUMN_TYPE, DBHelper.COLUMN_TRAVEL, DBHelper.COLUMN_SPEED};
String whereClause = DBHelper.COLUMN_TYPE + " = ? AND " + DBHelper.COLUMN_DATE + " BETWEEN " + dateOne + " AND " + dateTwo;
String[] whereArgs = {"Trip"};
Cursor cursor = database.query(DBHelper.TABLE_NAME_GPS, columNames, whereClause, whereArgs, null, null, DBHelper.COLUMN_NAME + " ASC");
while (cursor.moveToNext()) {
ModelGps gps = new ModelGps();
gps.setId(cursor.getLong(cursor.getColumnIndex(DBHelper.COLUMN_ID)));
gps.setName(cursor.getString(cursor.getColumnIndex(DBHelper.COLUMN_NAME)));
gps.setLatitude(cursor.getDouble(cursor.getColumnIndex(DBHelper.COLUMN_LATITUTE)));
gps.setLongitude(cursor.getDouble(cursor.getColumnIndex(DBHelper.COLUMN_LONGITUDE)));
gps.setAltitude(cursor.getDouble(cursor.getColumnIndex(DBHelper.COLUMN_ALTITUDE)));
gps.setDate(cursor.getString(cursor.getColumnIndex(DBHelper.COLUMN_DATE)));
gps.setType(cursor.getString(cursor.getColumnIndex(DBHelper.COLUMN_TYPE)));
gps.setTravel(cursor.getString(cursor.getColumnIndex(DBHelper.COLUMN_TRAVEL)));
gps.setSpeed(cursor.getString(cursor.getColumnIndex(DBHelper.COLUMN_SPEED)));
gpses.add(gps);
}
database.close();
cursor.close();
return gpses;
}