Search code examples
androidcursordistinctclause

Getting unique rows from column sqlite android cursor


Hi I cant get unique rows tried this from the documentation:

public Cursor getcepaUnico(){
return database.query(true, "vino", new String[] {"_id", "cepa"}, null, null, null, null, "cepa", null);}

but shows duplicated rows even if the DISTINCT boolean is changed. Also tried this:

public Cursor getCepaUnico() {
return database.rawQuery("select DISTINCT cepa from vinos", null);}

And the app crash after calling the method.


Solution

  • Setting distinct to true should have returned distinct results. Is it possible that your code which loops through the cursor is incorrect? You might want to post that also for review.

    Regarding your rawQuery, you are using a different table name which is probably what is causing the crash. It should be "select DISTINCT cepa from vino" (not vinos) to match your query statement.