Reputation: 417
What would be the android room query to match a column data exactly match or start with the search string
VVV this seems to giving me only the exact match
select * from table where dna_sequence like :searchTerm
Thanks
Upvotes: 1
Views: 3108
Reputation: 83537
this seems to giving me only the exact match
select * from table where dna_sequence like :searchTerm
There is nothing wrong with your query. The problem is with your Java or Kotlin code. You are getting an exact match because you are passing an exact to searchTerm
and not using any wildcards. The LIKE
operator in SQL allows us to use wildcards in the string to match more than just strict equality. _
will match a single character while %
will match zero or more characters. If you don't use these wildcards, then LIKE
will be the exact same as =
. So you need to pass a value for searchTerm
that uses one of these wild card characters. For example, if you a DAO interface declared as
@Dao
public interface MyDao {
@Query("SELECT * FROM foo WHERE search like :searchTerm")
public Foo[] getFooSearch(String searchTerm);
}
You can call the method with something like:
MyDao dao;
dao.getFooSearch("abcdef%");
which will match the start of the string.
Note: This example is a bit contrived because you only provided your SQL query and didn't provide any of the Java or Kotlin code for your DAO or Data Entity.
Upvotes: 2
Reputation: 329
Try this in search method:
public String getResultForSearchString(String searchTerm){
String searchExptression=searchTerm+"%";
//.. perform search operation on SQLite with searchExpression
return result;
}
For more information visit: https://www.sqlitetutorial.net/sqlite-like/
Hope it helps.
Upvotes: 2