使用静态变量更新 room db android
我找不到在 DAO 方法中使用静态变量而不通过方法本身传递它们的方法。
示例:
这有效:
@Transaction
@Query("UPDATE OpsPrinterDevice SET connectionStatus=:from WHERE connectionStatus=:to")
abstract fun switchDevicesConnection(from: String = Parameters.CONNECTED,to: String = Parameters.DISCONNECTED)
这不起作用:
@Transaction
@Query("UPDATE OpsPrinterDevice SET connectionStatus=${Parameters.CONNECTED} WHERE connectionStatus=${Parameters.DISCONNECTED}")
abstract fun switchDevicesConnection()
I can't find a way to use my static variables inside the DAO method, without passing them through the method itself.
example:
This works:
@Transaction
@Query("UPDATE OpsPrinterDevice SET connectionStatus=:from WHERE connectionStatus=:to")
abstract fun switchDevicesConnection(from: String = Parameters.CONNECTED,to: String = Parameters.DISCONNECTED)
This does not work:
@Transaction
@Query("UPDATE OpsPrinterDevice SET connectionStatus=${Parameters.CONNECTED} WHERE connectionStatus=${Parameters.DISCONNECTED}")
abstract fun switchDevicesConnection()
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
后者不起作用的原因是 文字值的 SQLite 语法。您需要将字符串值括在单引号中,例如
:passed_variable
)会自动正确封装并提供针对 SQL 注入的保护。The reason why the latter does not work is due to the SQLite syntax for literal values. You need to enclose a string value in single quotes e.g.
:passed_variable
) are automatically correctly enclosed and afforded protection against SQL Injection.