使用静态变量更新 room db android

发布于 2025-01-09 00:03:44 字数 522 浏览 3 评论 0原文

我找不到在 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

淡淡绿茶香 2025-01-16 00:03:44

后者不起作用的原因是 文字值的 SQLite 语法。您需要将字符串值括在单引号中,例如

@Query("UPDATE OpsPrinterDevice SET connectionStatus='${Parameters.CONNECTED}' WHERE connectionStatus='${Parameters.DISCONNECTED}'")
  • 但是,建议不要使用嵌入变量(而不是绑定变量)。绑定值(也称为使用 :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.

@Query("UPDATE OpsPrinterDevice SET connectionStatus='${Parameters.CONNECTED}' WHERE connectionStatus='${Parameters.DISCONNECTED}'")
  • However, it is recommended that embedding variables, as opposed to binding them, is not used. Bound values (aka using :passed_variable) are automatically correctly enclosed and afforded protection against SQL Injection.
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文