0

I am searching in sqlite database by a editext input. In the sqlite I have some sentence. I want to find the sentences from sqlite which there is in them some words which start by my input text. For example if my input text be: 'harm' I want to find the sentences which have words like 'harmful'. I don't want to find 'harmful' word when input search was 'ful'. Because this word isn't in the start of my word(harmful). Hope to get my purpose. Here is my example code:

String pattern = "'%" + inputSearch.getText().toString() + "%'";

                    final String sql = "SELECT * FROM " + TABLE + " WHERE " + TITLE  + " LIKE " + pattern;                      

                    final SQLiteDatabase mydb = new MyDatabase(ArdicActivity.this).getWritableDatabase();
            //      final Cursor c = mydb.rawQuery(sql, null);

I want to find the word between sentence. For example I want to find the 'harmful' word from sentence: 'this food is hurmful for you'

2 Answers2

0

Just remove the initial wildcard:

String pattern = "'" + inputSearch.getText().toString() + "%'";

Reference - SQLite LIKE condition

Also, I'd recommend you use the methods present on the SQLiteOpenHelper class instead of using concatenated Strings for your queries. See this answer

PPartisan
  • 8,173
  • 4
  • 29
  • 48
0

Wouldn't it work if you add a leading space into your pattern?

 String pattern = "'% " + inputSearch.getText().toString() + "%'";

Notice the space after the first percentage mark. This finds the sentences where the input is in the middle of the sentence. Then you can add another condition where the pattern is

String pattern = inputSearch.getText().toString() + "%'";

to find sentences starting with the input.

Pete
  • 280
  • 3
  • 7