skip to Main Content

I’m trying to get all value of column ‘group’ from table ‘group’ at android room-database.
Here is table ‘group’ at my android room-database.
enter image description here

And this is Dao. I want to use getGroupName()
enter image description here

But when I use this function at MainActivity and test by print it,
result is
enter image description here

I think column name is printed…
But I don’t know what to do.

What should I do if I want to get only values of column ‘group’??
For example, in this case,
[group1, group2, groupA]

2

Answers


  1. I might be wrong, but I think the query itself is the problem

    In SQL, single quotes are used to denote string literals, not column names.

    Therefore, when you’re trying to select the ‘color’ column from the ‘group’ table, SQL is interpreting ‘color’ as a string literal, not a column name.

    try this:

    @Query("SELECT color FROM group WHERE group = :groupName")
    fun getGroupColor(groupName: String): String
    
    Login or Signup to reply.
  2. I agree with the above answer https://stackoverflow.com/a/77204367/8432542

    My edition –

    I think, There are a couple of issues with your Room DAO class code snippet:

    1. Incorrect quotation marks:
      You are using single quotes around table and column names (‘color’ and ‘group’). In SQL, single quotes are used to denote string literals, not table or column names. You should use backticks (`) to enclose table and column names if needed, especially if they contain spaces or keywords.

    2. Using reserved keywords:
      "group" is a reserved keyword in SQL, and it’s generally not a good practice to use reserved words as table or column names. If you insist on using it, you can use backticks to escape it.

    Here’s a corrected version of your code:

    @Query("SELECT `color` FROM `group` WHERE `group` = :groupName")
    fun getGroupColor(groupName: String): String
    

    This should work as expected in your Android application.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search