adding DISTINCT keyword to query() with SQLite in Android adding DISTINCT keyword to query() with SQLite in Android database database

adding DISTINCT keyword to query() with SQLite in Android


Instead of the...

public Cursor query(String table, String[] columns, String selection,                    String[] selectionArgs, String groupBy, String having,                     String orderBy)

...method you're using, just use the...

public Cursor query (boolean distinct, String table, String[] columns,                      String selection, String[] selectionArgs, String groupBy,                     String having, String orderBy, String limit)

...overload and set distinct to true.

The Android docs seem a bit hard to direct link, but the doc page describing both is here.


you can use this,

Cursor cursor = db.query(true, YOUR_TABLE_NAME, new String[] { COLUMN1 ,COLUMN2, COLUMN_NAME_3 }, null, null, COLUMN2, null, null, null);

Here first parameter is used to set the DISTINCT value i.e if set to true it will return distinct column value.

and sixth parameter denotes column name which you want to GROUP BY.


You should use another QUERY function with first DISTINCT boolean parameter set to TRUE

public Cursor query (boolean distinct, String table,...)