9

In the Android Rooms persistence library, how would I write the following SQL statement as a @Query?

SELECT * FROM table WHERE field LIKE %:value%

This syntax is invalid, and I can't find anything about it in the documentation.

3

2 Answers 2

34

You can just concatenate using SQLite string concatenation.

@Query("SELECT * FROM table WHERE field LIKE '%' || :value  || '%'")
2
  • 1
    what if I want the field starts with :value? Thank you :) Oct 22, 2017 at 22:26
  • @AhmedMostafa @Query("SELECT * FROM table WHERE field LIKE :value || '%'")
    – Oktawian
    Mar 20, 2021 at 8:30
4

The answer by yigit works great for me:

@Query("SELECT * FROM stores " +
        "WHERE name LIKE '%' || :search  || '%' " +
        "OR description LIKE '%' || :search  || '%'")

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.