Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to select distinct values from one column in table?

Tags:

android

sqlite

I am developing a restaurant menu app in Android. My app has one database table which has the following columns:

  • id (primary key)
  • category
  • item name

The category column shows the category of item such as veg, non veg, snacks etc. It has duplicate values and I want to select only distinct values from this column. I have tried the following but it is not working if anyone can provide a solution:

String query = "SELECT DISTINCT category FROM todo";

Cursor  cursor = database.rawQuery(query,null);
if (cursor != null) {
    cursor.moveToFirst();
}

return cursor; 
like image 486
sagar Avatar asked Sep 13 '11 14:09

sagar


1 Answers

You can also use this specific query-Method of the SQLiteDatabase class that takes a boolean value to determine whether you want distinct values or not:

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

Link to Android Reference

This way you don't have to use rawQuery at all.

like image 161
Bashorings Avatar answered Nov 15 '22 19:11

Bashorings