0

The following code gives all values from database even duplicate values.

return database.query("contacts", new String[] {"_id", "name"}, 
     null, null, null, null, "name");

how can i get distinct values from above code

2
  • You should probably use rawQuery instead of query and use a real SQL query with the DISTINCT keyword to perform this action. See also a similar question here. Commented Feb 15, 2013 at 11:45
  • 1
    @JohnWillemse: Why not use the overload of query as indicated by Stefan's or my answer? Commented Feb 15, 2013 at 11:50

3 Answers 3

1

Use distinct keyword this will return unique record from database.

db.rawQuery("Select DISTINCT from table_name",null);
Sign up to request clarification or add additional context in comments.

Comments

1

Use the overload of query that takes a boolean as first parameter and remove the _id column from the select:

return database.query(true, "contacts", new String[] {"name"}, 
                      null, null, null, null, "name", null);

The _id column is supposed to be unique, so including it in your query will lead to the distinct having no effect.

1 Comment

of course it does. Becouse you select the primarey key "_id" also. That _id is always unique...
0

use this query:

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

set the first param to true and do not use youre primarey key in selection. it will be always unique...

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.