Pregunta
Tengo una aplicación de Android que utiliza una base de datos sqlite local.
private SQLiteDatabase mDb;
cuando ejecuto esta consulta, obtengo mi cursor sobre las filas con pid igual a id, según lo deseado:
mDb.query(true, PT_TABLE, new String[] {KEY_PID, KEY_TID},
KEY_PID+" = "+id, null, null, null, null, null);
cuando ejecuto la siguiente consulta, con el objetivo de obtener el mismo conjunto de resultados, ordenado por pid obtengo " android.database.sqlite.SQLiteException: falta de coincidencia de tipo de datos "
mDb.query(true, PT_TABLE, new String[] {KEY_PID, KEY_TID},
KEY_PID+" = "+id, null, null, null, null, KEY_PID+" DESC");
¿Alguna idea?
Solución
Parece que te confundiste un poco. De acuerdo con SQLiteDatabase.query
, el último argumento es la cláusula LIMIT
. El segundo al último es la cláusula ORDER BY
.
Cursor query (boolean distinct,
String table,
String[] columns,
String selection,
String[] selectionArgs,
String groupBy,
String having,
String orderBy, // <-- ORDER BY
String limit)
EDIT
Pero, también hay otro SQLiteDatabase.query
donde ORDER BY
sería el último
Cursor query (String table,
String[] columns,
String selection,
String[] selectionArgs,
String groupBy,
String having,
String orderBy)
Otros consejos
Esto me funcionó
String filter = MySQLiteHelper.JOB_ID + "=" + Integer.toString(jobID);
String orderBy = MySQLiteHelper.LOG_TIME + " DESC";
Cursor cursor = database.query(MySQLiteHelper.LOG_TABLE_NAME, logTableColumns,
filter, null, null, null, orderBy);
KEY_PID + " = " + "'" + id + "'"
Dado que Orderby es el segundo último parámetro en la consulta; tu consulta sería así
mDb.query(true, PT_TABLE, new String[] {KEY_PID, KEY_TID},
KEY_PID+" = "+id, null, null, null, KEY_PID+" DESC", null);
Si entendí correctamente tu problema, prueba esto.
String[] columns = new String[] {KEY_PID, KEY_TID};
String where = KEY_PID + " = " + Integer.toString(id);
String orderBy = KEY_PID + " DESC";
Cursor cursor = mDb.query(PT_TABLE, columns, where, null, null, null, orderBy);