1
WHERE g.value like '" + lookingFor + "%'

I have an EditText which filters cursor from SQLite database. I want to take 'value' from 'g' table. For example one of 'value' is 'full stop'. When I input 'stop', query should find full stop.

If lookingFor is "efg", nothing is found. How to find "abcd efgh" when "efg" is typed?

PS. I do not want to get "abcd edfh" when I type "cd"

MPelletier
  • 16,256
  • 15
  • 86
  • 137
Joe Rakhimov
  • 4,713
  • 9
  • 51
  • 109
  • Can you explain further? – midhunhk Aug 13 '13 at 09:38
  • I think you might need a % at the start of the string as well, such as like '%" + lookingFor + "%'. Your code would be looking for a field that starts with efg and ends in anything – CurlyPaul Aug 13 '13 at 09:41
  • Unfortunately, it will find "abcd efgh" when I type "cd", right – Joe Rakhimov Aug 13 '13 at 09:47
  • It will. Didn't see your edit until after I posted. So why not include the space in the query eg LIKE '% " + lookingFor + "%'. This should find a field that has some characters at the start, followed by a space, followed by what you typed – CurlyPaul Aug 13 '13 at 09:54

4 Answers4

3

Try using
WHERE g.value like '%" + lookingFor + "'

hich9n
  • 1,578
  • 2
  • 15
  • 32
1

The % before the lookingFor in sql represents that there might be other data before lookingFor. % after the lookingFor represents that there might be other data after lookingFor.

So if you want to find "efg" when you have data like "abcd efgh" you need to do:

....WHERE g.value LIKE '%" + lookingFor + "%' ";

And yes unfortunately it will find "abcd.." when you type "cd". In order to fix this you can check the value of lookingFor before you actually execute the sql command.

if(lookingFor.equals("efg")
{
    ....WHERE g.value LIKE '%" + lookingFor + "%' ";
}
else
{
     //Do whatever you want here if the value is not "efg".
}

Good luck

ClaireG
  • 1,244
  • 2
  • 11
  • 23
0

Try this one :

Cursor oCursor = myDatabasequery(true, DATABASE_TABLE, new String[]{column1, column2,column3},  column2+ " LIKE '%" + lookingFor + "%'", null, null, null, null, null);

In your code :

... WHERE g.value LIKE '%" + lookingFor + "%' "

It will search your string whatever before and after that

Harish Godara
  • 2,388
  • 1
  • 14
  • 28
0

Before query:

String strSpace=" ";

Inside query:

 "...   WHERE g.value like '"+ lookingFor + "%' OR g.value like '%"+strSpace + lookingFor + "%'   ..."
Joe Rakhimov
  • 4,713
  • 9
  • 51
  • 109