Domanda
Ho un'app Android che utilizza un database sqlite locale.
private SQLiteDatabase mDb;
quando eseguo questa query ottengo il mio cursore su righe con pid uguale a id, come desiderato:
mDb.query(true, PT_TABLE, new String[] {KEY_PID, KEY_TID},
KEY_PID+" = "+id, null, null, null, null, null);
quando eseguo la seguente query, con l'obiettivo di ottenere lo stesso set di risultati, ordinato per pid ottengo " android.database.sqlite.SQLiteException: mancata corrispondenza del tipo di dati "
mDb.query(true, PT_TABLE, new String[] {KEY_PID, KEY_TID},
KEY_PID+" = "+id, null, null, null, null, KEY_PID+" DESC");
Qualche idea?
Soluzione
Sembra che tu abbia fatto un po 'di confusione. Secondo SQLiteDatabase.query
, l'ultimo argomento è la clausola LIMIT
. La penultima è la clausola ORDER BY
.
Cursor query (boolean distinct,
String table,
String[] columns,
String selection,
String[] selectionArgs,
String groupBy,
String having,
String orderBy, // <-- ORDER BY
String limit)
Modifica
Ma c'è anche un altro ORDER BY
sarebbe l'ultimo
Cursor query (String table,
String[] columns,
String selection,
String[] selectionArgs,
String groupBy,
String having,
String orderBy)
Altri suggerimenti
Questo ha funzionato per me
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 + "'"
Poiché Orderby è il secondo ultimo parametro nella query; la tua query sarebbe così
mDb.query(true, PT_TABLE, new String[] {KEY_PID, KEY_TID},
KEY_PID+" = "+id, null, null, null, KEY_PID+" DESC", null);
Se ho compreso correttamente il tuo problema, prova questo.
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);