开发者

How to query similar records in SQLite database from Android?

开发者 https://www.devze.com 2023-03-03 13:24 出处:网络
Let\'s say an SQLite database column contains the following value: U Walther-Schreiber-Platz Using a query I\'d like to find this record, or any similar records, if the user searches for the followin

Let's say an SQLite database column contains the following value:

U Walther-Schreiber-Platz

Using a query I'd like to find this record, or any similar records, if the user searches for the following strings:

  • walther schreiber
  • u walther
  • walther-schreiber platz
  • [Any other similar strings]

However I cannot figure out any query which would do that. Especially if the user does not enter the - character.

Example:

select * from myTable where value like '%walther schreiber%'; would not return the result because of the 开发者_JAVA技巧missing -.

Thanks, Robert


So, as I said in my comment, I think you can have a query along the lines of:

select * from myTable where LOWER(value) like <SearchValue>

I'm assuming you're collecting the <SearchValue> from the user programmatically, so would be able to do the following: <SearchValue> would need to be: The user's search string, appropriately cleansed to avoid SQL injection attacks, converted to lower case, with all of the spaces converted to '%', so that they match zero or more characters...

So you would have (for example):

select * from myTable where LOWER(value) like '%walther%schreiber%'
select * from myTable where LOWER(value) like '%walther-schreiber%platz%'

etc... however, this does assume that the word order is the same, which in your examples it is...

0

精彩评论

暂无评论...
验证码 换一张
取 消