How to select data between two date ranges in android SQLite

I have a table that contains data along with the created by date. I want to select data from a table according to two given date ranges or one specific month.

I'm not sure how to do this, since the text created in the column is of type and saved as dd-MM-yyyy HH: mm: ss

I am using rawQuery () to retrieve data.

+6
source share
2 answers

You can do something like this:

mDb.query(MY_TABLE, null, DATE_COL + " BETWEEN ? AND ?", new String[] { minDate + " 00:00:00", maxDate + " 23:59:59" }, null, null, null, null); 

minDate and maxDate , which are date strings, make up your range. This query will get all rows from MY_TABLE that fall between this range.

+12
source

Here are a few useful

  //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; } 
0
source

Source: https://habr.com/ru/post/910419/


All Articles