Character start with query in room persistent android

Viewed 2817

I have tried the following query but it does not give any result.

SELECT * FROM details WHERE name LIKE :name

I have used AutoCompleteTextview for searching purpose. is there any query to find with a substring?

3 Answers

I recently ran into this issue and found the nicest solution is to use Sql string concatenation || in combination with % which represents zero, one, or multiple numbers or characters.

@Query("SELECT * FROM details WHERE name LIKE '%' || :name || '%'")
fun getDetails(name: String): details

This will get the details where the name contains the one we are looking for

Similarly the below will find matches that start with the name

LIKE :name || '%'

Example:

@Query("SELECT * FROM user WHERE first_name LIKE :firstName ")
User findByName(String firstName);

I think u must use different name for "name".

Related