Pankaj Kumar Pankaj Kumar - 3 months ago 10
Android Question

How to pass two or more selection argument in "query" method

I am using query method, but I don't no how to pass more than one selection argument in query method.

My query method should return result as same as this sql statement :


SELECT _id FROM CONATCT_TAGS WHERE TAG1='tagname' OR
TAG2='tagname' OR
TAG3='tagname' OR
TAG4='tagname' OR
TAG5='tagname';

Answer

Try something like that..

SQLiteDatabase db = YourDatabaseHelper.getReadableDatabase();
String TABLE = "CONTACT_TAGS";
String[] FIELDS = { "_id" };
String WHERE =  "TAG1='tagname' OR TAG2='tagname' OR TAG3='tagname' OR TAG4='tagname' OR TAG5='tagname' ";
// Execute
cursor = db.query(TABLE, FIELDS, WHERE, null, null, null, null);

If you read the documentation of the Cursor.query() method you find the definition for selection as follows:

selection: A filter declaring which rows to return, formatted as an SQL WHERE clause (excluding the WHERE itself). Passing null will return all rows for the given table.

Comments