Android: consulta con fecha

I must to do a query but I have a problem with date becase the structure is 27/08/2011 this is the code :

dt="27/08/2011";
db.query(TABLE,null,mydata + "=" + dt,null,null,null,null);

the problem is that the cursor is always 0. I think that is the data structure because if I query other it work fine

preguntado el 27 de agosto de 11 a las 21:08

1 Respuestas

Your date isn't quoted so SQLite will interpret 27/08/2011 as a mathematical expression and replace it with 0 (the integer value of 27 divided by 8 divided by 2011). You could quote it yourself:

db.query(TABLE, null, mydata + "=" + "'" + dt + "'", null, null, null, null);

O mejor, use a placeholder:

db.query(TABLE, null, mydata + "= ?", new String[] { dt }, null, null, null);

Cambiar a un ISO 8601 date format would also be a good idea to avoid possible locale-based ambiguity in the date:

String dt = "2011/08/27";
Cursor c  = db.query(TABLE, null, mydata + "= ?", new String[] { dt }, null, null, null);

Respondido 28 ago 11, 01:08

THanks for your answer. And with your method if I want select mydata and myid how is the code ? - Mimmo

@Mimmo: mydata is a column name rather than a String variable? If yes then db.query(TABLE, new String[] { "mydate", "myid" }, "mydata = ?", ... - mu es demasiado corto

This is right ?? String dt = "2011/08/27"; String n = "ciao"; Cursor c = db.query(TABLE, null, mydata + "= ? && " + myid + "=?", new String[] { dt,n }, null, null,null); - Mimmo

I want to do this : SELECT * FROM TABLE WHERE mydata='dt' && myid='n' how can I do this ?? - Mimmo

@Mimmo: I think you're looking for db.query(TABLE, null, "mydata = ? AND myid = ?", new String[] { dt, n }, ... dónde n es una cadena. - mu es demasiado corto

No es la respuesta que estás buscando? Examinar otras preguntas etiquetadas or haz tu propia pregunta.