问题描述
我看到了这个问题.类似的错误.但就我而言,情况有所不同.
i have seen this question. Similar error.But in my case it is different.
与Room I一起工作时,正在创建桌子.工作正常.
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)}
但是后来我发现所有表名都必须存储在不同的类中.像表名用户"一样->存储在不同的类中.
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 示例.
这篇关于Annotation参数必须是一个编译时间常数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!