问题描述
我看过这个问题.类似的错误.但在我的情况下是不同的.
i have seen this question. Similar error.But in my case it is different.
在使用 Room 时,我正在创建表格.它工作正常.
While working with Room i was creating table. it was working fine.
@Daointerface
UserDao {
@Query("SELECT * FROM user")
fun getAll(): List<User>
@Insert(onConflict = OnConflictStrategy.REPLACE)
fun insert(user: User)
@Delete
fun delete(user: User)}
但后来我发现所有表名都必须存储在不同的类中.像表名user"-> 存储在不同的类中.
but then i found all table Names must be stored in a different class. like table name "user" - > Stored in different class.
例如
class Table {
companion object {
const val USER_TABLE = "user"
}}
但下面的代码不起作用.它没有从 Table 类中获取表名.给出编译时错误.Annotation 参数必须是编译时常量"请帮帮我.它有什么问题
But below code is not working . it is not picking up table name from Table class. Giving compile time error . "An Annotation argument must be a compile time constant" please help me out.What wrong in it
@Query("SELECT * FROM $Table.USER_TABLE")
fun getAll(): List<User>
推荐答案
问题在于错误中所述的问题,您不能为 @Query
注释动态定义参数.如果要在其他地方定义表的名称,请使用字符串连接.你可以这样做:
The problem is the one stated in the error, you can't have dynamically defined arguments for your @Query
annotation. If you want to define the name of the table somewhere else, use string concatenation. You can do it like this:
@Query("SELECT * FROM " + Table.USER_TABLE)
fun getAll(): List<User>
这就是他们在这个 google 示例.
This is how they do it in this google sample.
这篇关于Annotation 参数必须是编译时常量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!