How do I use WHERE with dates? SQLite

0

The question can surely sound very noob. But hey, it turns out that I want to execute a simple where with dates. I tried everything (until I changed the column from type DATE to type TEXT) and I still have no results.

 Cursor curs = admin.selectWhere(DBScheme.Tabla_Asignaciones,new String[]{
            DBScheme.Columna_IdMateria,
            DBScheme.Columna_Descripcion,
            DBScheme.Columna_Fecha_Creacion,
            DBScheme.Columna_Fecha_Inicio,
            DBScheme.Columna_Fecha_Expiracion,
            DBScheme.Columna_Entregado},
            DBScheme.Columna_Fecha_Expiracion + "=?",
            new String[]{tiempo.getFecha(dia)});

The part of the time.getDate (day) , is a function that returns the date (with the format yyyy-MM-dd) passing it as parameter the day of the week (Monday, Friday, Sunday ...)

It gives me something like this: 2017-12-25 for example. By the way, although I write it directly ("2017-12-25") it still does not work ... it returns 0 rows.

Update

The functions I use to execute queries:

  public Cursor selectLog(String query){
        SQLiteDatabase db = this.getReadableDatabase();
        Cursor cursor;
        cursor = db.rawQuery(query,null);
        return cursor;
    }

    public Cursor selectWhere(String table,String[]columns, String whereCols, String[]whereArgs){
        SQLiteDatabase db = this.getReadableDatabase();
        Cursor cursor = db.query(table,columns,whereCols,whereArgs,null,null,null);
        return cursor;
    }

With the last suggestion that they gave me, by the way, the first code that I showed of this question, is as follows:

Cursor curs = admin.selectLog("SELECT * FROM " + DBScheme.Tabla_Asignaciones +
    " WHERE " + DBScheme.Columna_Fecha_Expiracion + "='" + tiempo.getFecha(dia) + "'");
    
asked by TwoDent 24.12.2017 в 22:09
source

1 answer

0

Maybe you can solve it this way: Update the response:

public Cursor selectWhere(String fecha){
        SQLiteDatabase db = this.getReadableDatabase();
        String query = "Select * from Tabla_Asignaciones where fecha = '" + fecha + "';";
        //ejecutas la consulta
        Cursor cur = db.rawQuery(query, null);
        //Al terminar cierras la BD
        db.close();
        return cur; 
    }  

Then you move the cursor to get the data:

//Aqui puedes cambiar el parametro segun tus necesidades
Cursor cur = selectWhere('2017-12-15') 
if(cur.moveToFirst()){
   do {
        String nombre = cur.getString(0);
        String fecha = cur.getString(1);
       //Aqui puedes guaradar la informacion en un objecto, etc
    } while (cursor.moveToNext());
 } 

Comment if it has served you!

    
answered by 24.12.2017 в 22:34