How to select individual values ​​from one column in a table?

I am developing a restaurant menu application on Android. My application has one database table that has the following columns:

  • id (primary key)
  • category
  • item name

The category column shows the product category, such as veg, non veg, snacks, etc. It has duplicate values, and I want to select only individual values ​​from this column. I tried the following, but it does not work if someone can provide a solution:

String query = "SELECT DISTINCT category FROM todo"; Cursor cursor = database.rawQuery(query,null); if (cursor != null) { cursor.moveToFirst(); } return cursor; 
+6
source share
4 answers

You can also use this special query method of the SQLiteDatabase class, which takes a boolean value to determine if you want excellent 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 Link

This way you do not need to use rawQuery.

+11
source

Try:

 "SELECT DISTINCT category as _id, category FROM todo" 

It worked for me in the same situation.

+6
source

Here's how I make it clear as well as getting categories

 // Get Categories public Cursor getCategories() { Cursor c = db.rawQuery("SELECT DISTINCT " + KEY_ITEM_CAT + " as " + KEY_ITEM_ID + ", " + KEY_ITEM_CAT + " FROM " + ITEMS_TABLE_NAME, null); if (c != null) { c.moveToFirst(); } return c; } 
+1
source

I looked for the same question, and found something direct in the solution in the simplest way! Check this answer:

fooobar.com/questions/208373 / ...

0
source

Source: https://habr.com/ru/post/897238/


All Articles