Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

select distinct value in android sqlite

Tags:

android

sqlite

I try to run the following raw query in android, it seems not work

String query ="SELECT DISTINCT category FROM event"; Cursor  cursor = mDb.rawQuery(query, null); if (cursor != null) {      cursor.moveToFirst(); } return cursor;  

so I decide to use the query() method in android which are something like

Cursor mCursor = mDb.query(EVENT_TABLE, new String[] {KEY_ROWID, KEY_CAT}, null, null,null,null, null) 

Can anyone show me how to select the distinct category for using query() instead of rawquery please, any help will be greatly appreciated!

like image 660
smith Avatar asked Jun 27 '12 04:06

smith


1 Answers

But you MUST remember to send argument in GROUPBY (NOT NULL send).

You must give column name for distinct.

Example:

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

true - distinct TRUE

COLUMN_NAME_2 - name column what you have be DISTINCT.

That's works for me fine.

like image 175
Adam Kowalski Avatar answered Oct 23 '22 15:10

Adam Kowalski