简体   繁体   中英

Why Android Room @Query LIKE is not returning data known to exist

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.

房间查询注解代码

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM