How to use a LIKE clause in a query function

I am making an application that searches for messages in the inbox based on the sender number. Here is my code:

public void onClick(View v) { Toast.makeText(this, "search", Toast.LENGTH_LONG).show(); final Uri SMS_INBOX = Uri.parse("content://sms/inbox"); String[] colList = {"address", "body"}; Cursor c = getContentResolver().query(SMS_INBOX, colList, null, null,"DATE desc"); String yz= String.valueOf(c.getCount()); Toast.makeText(this, yz, Toast.LENGTH_LONG).show(); if(c.moveToFirst()) { Toast.makeText(this, searchtxt.getText(), Toast.LENGTH_LONG).show(); for(int i=0;i<c.getCount();i++) { String number=c.getString(c.getColumnIndexOrThrow("address")).toString(); boolean match = number.toUpperCase().indexOf(searchtxt.getText().toString()) != -1; if (match) { Toast.makeText(this, String.valueOf(i), Toast.LENGTH_LONG).show(); String body= c.getString(c.getColumnIndexOrThrow("body")).toString(); tv.setText(tv.getText() + "\n" + body); } c.moveToNext(); } } else { Toast.makeText(this, "Inside else", Toast.LENGTH_LONG).show(); } c.close(); } 

The above code works fine, but it retrieves all the messages in the inbox. I want him to receive only those messages that match the sender's number. To do this, I tried the query using the LIKE clause, but with that it returns 0 records. What is the correct way to use the LIKE clause. Here is the code I tried to use LIKE

 String[] colList = {"address", "body"}; String[] argList = {"'%"+searchtxt.getText().toString()+"%'"}; Cursor c = getContentResolver().query(SMS_INBOX, colList, "address LIKE ?", argList,"DATE desc"); String yz= String.valueOf(c.getCount()); Toast.makeText(this, yz, Toast.LENGTH_LONG).show(); 

Please help me .... Thanks ...

+6
source share
3 answers

There are two pranksters: "%" and "_".

"%" replaces any string (multiple characters),

"_" replaces one (and only one) character.

For instance:

 LIKE 'B%' 

You will find everything that starts with "B". (for example, "BATMAN");

 LIKE '%B%' 

allows you to find everything that contains "B". (e.g. "ABBA");

 LIKE'_B%' 

let you find everything that contains ONE character, and then "B". (e.g. "ABERCROMBIE").

if you want to get a string containing "%" or "_", you can use the escape code:

 LIKE '%#_B%' ESCAPE # 

you can find any lines containing one "_" followed by a "B". (e.g. "TEST_BATMAN").

I hope this helps.

Al_th

+7
source

Try to execute EDITED :

 String[] argList = {"'%%%%"+searchtxt.getText().toString()+"%%%%'"};//TRY THIS ^^^^ ^^^^ // TRY THIS Uri smsUri = Uri.parse("content://sms/inbox"); Cursor c = getContentResolver().query(smsUri, colList, "address LIKE ?", argList, "DATE"); 
+2
source

I also had this problem in my code, and it took me a while to find the correct answer. What worked for me is what @Arushi gupta wrote in his answer.

Here is my working code, adapted so that the user can perform EXACT search queries or search for values ​​that contain words in the query (AND operator or OR operator). My code uses CursorLoader, but it is similar to using ContentResolver for this example:

 String selection = myDbContract.myDbEntry.getSelectionForGivenKeywordsAndOperator(requested_keyword, "OR"); //Can write "EXACT" / "AND" / "OR" return new CursorLoader(this, myQueryUri, MY_TABLE_ELEMENTS, selection, null, null); 

And here is the selection method in the db contract:

  public static String getSelectionForGivenKeywordsAndOperator(String keyword_list, String operator) { String returnSQLCommand = null; if(keyword_list.length()==0) return returnSQLCommand; switch (operator) { case "EXACT": returnSQLCommand = COLUMN_KEYWORD + "='" + keyword_list + "'"; break; default: //operator is "AND" or "OR" List<String> keywords = new ArrayList<>(); if (keyword_list.contains(" ")) keywords = Arrays.asList(keyword_list.split(" ")); else if (keyword_list.contains(",")) keywords = Arrays.asList(keyword_list.split(",")); else if (keyword_list.contains(";")) keywords = Arrays.asList(keyword_list.split(";")); else if (keyword_list.contains(".")) keywords = Arrays.asList(keyword_list.split(".")); else keywords.add(keyword_list); returnSQLCommand = "'%" + keywords.get(0) + "%'"; if (keywords.size()>=1) { returnSQLCommand = COLUMN_KEYWORD + " LIKE '%" + keywords.get(0) + "%'"; for (int i = 1; i < keywords.size(); i++) { returnSQLCommand = returnSQLCommand + " " + operator + " " + COLUMN_KEYWORD + " LIKE '%" + keywords.get(i) + "%'"; } } break; } return returnSQLCommand; } 

I checked that this code works as planned, and try it.

0
source

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


All Articles