Best unofficial Apache Server developers community
Username
Forgot password?
Sign in with Twitter account
Sign in with Facebook account

Android Java SQLite searching / matching question

1

52 views

So I'm trying to search through a column where the string is a comma separated list of words with no spaces such as one,two,three,four and so on. Problem I'm facing is that I'm trying to match an exact word in the string. As of now I'm simply using %word% to find a match which of course works but the problem is it isn't exact in that if I searched for 'word' it would also match 'words', 'worded' and so on. So question is how can I search and get the exact word?

asked June 24, 2011 9:14 am CDT
posted via StackOverflow

3 Answers

1
 

Append and prepend a comma to the string value in the column and then search it for your term which also has comma on either side of it:

   one,two,three,twofold becomes ,one,two,three,twofold,

and you search for

   %,two,%


 where ',' + mycolumn + ',' like '%,two,%'

You don't have to alter ("tamper with") the database record itself.

answered June 24, 2011 9:24 am CDT
Tim
1
 

There is a design problem here. The database structure is not correct for what you're trying to achieve. Storing multiple values in a column and trying to match single values is possible with some other database as MySQL (with the SET type), but it's not on SQLite. So you could try all sort of weird string matching solution, but it won't be clean, and you will not be able to take advantage of SQLite optimizations. It will affect performance.

What you could use instead is a separate words table which contain a unique record for each word and another foobar_words table to establish a many-to-many relation between the words and your main foobar table. Then you could match individual words precisely by using a join. And if there are performance problems, with this schema you should be able to optimize with indexes.

answered June 24, 2011 5:32 pm CDT
0
 

If you don't want to mess with the string, you could modify your sql so that you are looking where mycolumn like 'word,%' or mycolumn like '%,word,%' or mycolumn like '%,word'

answered June 24, 2011 5:32 pm CDT

Your answer

Join with account you already have


Sign in with Twitter account
Sign in with Facebook account
Sign in with Google Friend Connect

Preview
Similar questions