Reputation: 82958
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';
Upvotes: 11
Views: 36982
Reputation: 191
You can use it like that
String strSelection = android.provider.CallLog.Calls.TYPE +
" = " + CallLog.Calls.MISSED_TYPE+
" OR " + android.provider.CallLog.Calls.TYPE +
" = " + CallLog.Calls.INCOMING_TYPE ;
Upvotes: 2
Reputation: 3242
Try this , this code i getting all documents files using media store, using single query with multiple arguments.
String pdf = MimeTypeMap.getSingleton().getMimeTypeFromExtension("pdf");
String doc = MimeTypeMap.getSingleton().getMimeTypeFromExtension("doc");
String docx = MimeTypeMap.getSingleton().getMimeTypeFromExtension("docx");
String xls = MimeTypeMap.getSingleton().getMimeTypeFromExtension("xls");
String xlsx = MimeTypeMap.getSingleton().getMimeTypeFromExtension("xlsx");
String ppt = MimeTypeMap.getSingleton().getMimeTypeFromExtension("ppt");
String pptx = MimeTypeMap.getSingleton().getMimeTypeFromExtension("pptx");
String txt = MimeTypeMap.getSingleton().getMimeTypeFromExtension("txt");
String rtx = MimeTypeMap.getSingleton().getMimeTypeFromExtension("rtx");
String rtf = MimeTypeMap.getSingleton().getMimeTypeFromExtension("rtf");
String html = MimeTypeMap.getSingleton().getMimeTypeFromExtension("html");
//Table
Uri table = MediaStore.Files.getContentUri("external");
//Column
String[] column = {MediaStore.Files.FileColumns.DATA};
//Where
String where = MediaStore.Files.FileColumns.MIME_TYPE + "=?"
+" OR " +MediaStore.Files.FileColumns.MIME_TYPE + "=?"
+" OR " +MediaStore.Files.FileColumns.MIME_TYPE + "=?"
+" OR " +MediaStore.Files.FileColumns.MIME_TYPE + "=?"
+" OR " +MediaStore.Files.FileColumns.MIME_TYPE + "=?"
+" OR " +MediaStore.Files.FileColumns.MIME_TYPE + "=?"
+" OR " +MediaStore.Files.FileColumns.MIME_TYPE + "=?"
+" OR " +MediaStore.Files.FileColumns.MIME_TYPE + "=?"
+" OR " +MediaStore.Files.FileColumns.MIME_TYPE + "=?"
+" OR " +MediaStore.Files.FileColumns.MIME_TYPE + "=?"
+" OR " +MediaStore.Files.FileColumns.MIME_TYPE + "=?";
//args
String[] args = new String[]{pdf,doc,docx,xls,xlsx,ppt,pptx,txt,rtx,rtf,html};
Cursor fileCursor = getActivity().getContentResolver().query(table, column, where, args, null);
while (fileCursor.moveToNext()) {
//your code
}
Upvotes: 0
Reputation: 10485
If you want to use the built-in optimization in the Android SQLite connection you could do something like this instead:
String table = "CONTACT_TAGS";
String[] columns = {"_id"};
String where = "TAG1=? OR TAG2=? OR TAG3=? OR TAG4=? OR TAG5=?";
String[] args = {"tagname", "tagname", "tagname", "tagname", "tagname"};
SQLiteDatabase db = myDatabaseHelper.getReadableDatabase();
Cursor cursor = db.query(table, columns, where, args, null, null, null);
The difference from @raultum's solution is that you let the database connector decide how to interpret the submitted data. This is extra interesting from a security perspective.
Upvotes: 50
Reputation: 716
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.
Upvotes: 11