0

No consigo filtrar por fecha de inicio fecha de fin sobre un SQLite.

Para ello he creado un método el cual se le pasa 2 fechas, una de inicio y otra de fin.

private void consultarListaDietas(Date di, Date df) {

    SQLiteDatabase db =conn.getReadableDatabase();
    Dieta dieta=null;
    Date date = new Date();
    Cursor cursor = db.rawQuery("SELECT * FROM "+Utilidades.TABLA_DIETAS,null);
    if (di==null && df==null){
        while (cursor.moveToNext()){
            dieta = new Dieta();
            dieta.setId_Dieta(cursor.getInt(0));
            dieta.setNoches(cursor.getInt(1));
            dieta.setKm(cursor.getDouble(2));
            String fecha = cursor.getString(3)
            DateFormat iso8601Format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            try {
                date = iso8601Format.parse(fecha);
            } catch (ParseException e) {
                Log.e(TAG, "Parsing ISO8601 datetime failed", e);
            }
            dieta.setFecha(date);
            dieta.setId_Usuario_FK(cursor.getInt(4));
            dieta.setId_Proyecto_FK(cursor.getInt(5));

        }

    }else{
        cursor=null;
        Date[] args = new Date[]{di,df};
        cursor = db.rawQuery("SELECT * FROM "+Utilidades.TABLA_DIETAS + " WHERE BETWEEN di AND df",args);
    }
}

El problema lo tengo en el else, en los args me aparece un error "Wrong 2nd argument type". He comprobado que tanto el argumento di como df son argumentos tipo Date. Supongo que tengo algún problema en mi consulta.

Gracias de antemano!!

1 respuesta 1

0

La firma del método rawQuery que estás usando es rawQuery(String sql, String[] selectionArgs), por lo tanto el segundo parametro debe ser un vector de String no de Date.

di y df, no están definidos en el código pero asumo que son Date, tendrian que ser tansformados a String de la forma "yyyy-MM-dd HH:mm:ss" para ser usados en el query.

El query tiene dos errores:

1) Falta el campo fecha de la tabla antes del BETWEEN
2) Los valores a reemplazar se marcan con signo de pregunta.

DateFormat iso8601Format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
String strDi = iso8601Format.format(di);
String strDf = iso8601Format.format(df);
String[] args = new String[]{strDi,strDf};
cursor = db.rawQuery("SELECT * FROM "+Utilidades.TABLA_DIETAS + " WHERE <CAMPO FECHA DE LA TABLA> BETWEEN ? AND ?",args);

Tu Respuesta

By clicking “Publica tu respuesta”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

¿No es la respuesta que buscas? Examina otras preguntas con la etiqueta o formula tu propia pregunta.