How to get the record with the date closest to the current one in sqlite?

0

my question is how can I do that in a sqlite query the record appears with the date closest to the current, only one, I try to fill a recyclerView but only should appear the data with the closest date, so It is my method to show the data:

private List<RegistroAuto> ObtenerLista() {
    SQLiteHelper base = new SQLiteHelper(getContext(),"Cartago.db",null,1);
    SQLiteDatabase datos = base.getReadableDatabase();
    List<RegistroAuto>lista= new ArrayList<>();
    RegistroAuto registroAuto = null;
    SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd", Locale.getDefault());
    Date date = new Date();
    String fecha = simpleDateFormat.format(date);
    Cursor cursor = datos.rawQuery("SELECT Marca,Placa,FecSoap,Modelo FROM tbl_regautosx",null);
    while (cursor.moveToNext()){
        registroAuto = new  RegistroAuto();
        registroAuto.setMarca(cursor.getString(0));
        registroAuto.setPlaca(cursor.getString(1));
        registroAuto.setFecSoap(cursor.getString(2));
        registroAuto.setModelo(cursor.getString(3));
        lista.add(registroAuto);
    }
    return lista;
}

works but only the one whose soat expiration date is closest (FecSoap) should appear, how can I do it, how do I propose the query or how do I compare them, in advance thanks?

    
asked by Harvey66 09.09.2018 в 04:12
source

2 answers

0

Why do not you do it in a query?

SELECT * FROM laTabla
WHERE  date(campoFecha) <= date(laFecha) 
ORDER BY date(campoFecha)
DESC Limit 1
    
answered by 09.09.2018 / 04:36
source
0

You can use query instead of rawquery :

 String[] columnas = new String[] {"Marca", "Placa", "FecSoap", "Modelo"};
 Cursor cursor = db.query("tbl_regautosx", columnas, null, null, null, null, FecSoap+" DESC LIMIT 1");

The parameters of the query are:

TableName, columns, where, args, group, having, order by.

Therefore the last parameter is to order, in this case based on the column of dates, in descending order (from closer to farther) and showing only one result ( LIMIT 1 ). If you want all the results to be shown, just add " DESC" Respect the space before DESC ( " DESC ... ).

    
answered by 09.09.2018 в 06:42