5

My problem: I'm not sure why this Android Room query is not returning results. I thought I understood the proper coding and it seems quite simple. If I pass this same query directly to the same database using SQLiteStudio, I get results.

The calling code (I hardcoded the string 'truth' to test it):

List<Integer> tableIDs = researchDatabase.getCommentsDao().customSearchCommentsTable("truth");

The DAO code (Contained in the CommentsDao):

@Query("SELECT CommentID FROM Comments WHERE Comment LIKE :value")
List<Integer> customSearchCommentsTable(String value);

What I have done: I'm new to Android Room, but I have been using all examples and lessons from Android Developers (developer.android.com) and reviewed and applied many posts here at stackoverflow closely related but I cannot get any results to return. When I step through the code, the actual Android Room code does not seem to be binding the variable string to the statement though I can see the argument string being identified and passed properly, which isn't code that I have written, or at least I never see the resulting binding string with my variable data, I only see this:

SELECT CommentID FROM Comments WHERE Comment LIKE ?

What my goal is: This is actually the first step to creating a query to handle a multiple LIKE query similar to this, which I think will require a @RawQuery setup. Right now, I cannot get the simple thing to work.

What happens in the @Query wrapper: I realize I may be naive and wrong here, but below I can see the argument and statement both get passed, but it does not appear as if the "_statement.bindString" is actually binding the ":value" ('truth') to the statement and is returning 0 results.

Room Query Annotation code

3
  • FYI to viewers: a ticket was opened on this issue on 2/14/20 and later forwarded to Room development on 3/2/20 as a possible defect. Any update to this issue I will post here when resolved. (Ticket URL: issuetracker.google.com/149519050) Commented Mar 2, 2020 at 14:51
  • 1
    FYI to viewers: Room development denied there was an issue in the ticket I raised and in essence stated the example provided shouldn't be taken literally. They provided a solution that should be used which involved concatenating wildcards. In my opinion, 99% of all official Android Room development code is provided to be taken literally and to teach, other wise, what's the purpose of providing it. With respect to their response and my opinion, this still is a bug they refuse to correct in documentation or programatically. Commented Mar 10, 2020 at 16:21
  • Get used to this kind of thing. Google's handling of Android bug reports can only be characterized as grossly incompetent. Commented Mar 10, 2020 at 18:55

1 Answer 1

6

This is the solution provided by the Android Room development team:

@Query("SELECT CommentID FROM Comments WHERE Comment LIKE '%' || :value || '%'")
List<Integer> customSearchCommentsTable(String value);

The irony is that the development team could not get their posted code example to work. By the time they responded I had already created a workaround using a @RawQuery method to create the proper SQL syntax.

2
  • Thanks. This is the only working answer. Helped me a lot. Commented Jul 27, 2021 at 23:43
  • Amazing answer bro Commented Apr 29 at 9:40

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