简体   繁体   English

如何在android房间数据库sql中使用replace?

[英]How to use replace in android room database sql?

    @Query("update bookmarks set bookmarks_path= replace(bookmarks_path,:oldPath,:newPath) where bookmarks_path like :oldPath || '%'")
    fun updateBookmarksPath(oldPath: String, newPath: String): Completable

I am using the room database and this is my code我正在使用房间数据库,这是我的代码

I found that the format of this code is not correct when running, android studio prompts me: expression expected, got'replace'运行时发现这段代码格式不正确,android studio提示我:expression expected, got'replace'

like this像这样

在此处输入图像描述

I want to achieve the effect like this我想达到这样的效果

----path------------------path -----路径--------路径

/root/1/1--------------/stack/1/1 /root/1/1--------------/stack/1/1

/root/name/1------/stack/name/1 /root/name/1------/stack/name/1

/root/cls/1-----------/stack/cls/1 /root/cls/1-----------/stack/cls/1

/root/go/1-----------/stack/go/1 /root/go/1-----------/stack/go/1

/root/js/1------------/stack/js/1 /root/js/1------------/stack/js/1

please

REPLACE is both an SQLite keyword and, in the case of your intended use, the name of a string function: replace() . REPLACE既是SQLite 关键字,又是字符串 function 的名称: replace() The Room annotation processor appears to be rejecting your query string because it is treating replace as keyword instead of function name. Room 注释处理器似乎拒绝您的查询字符串,因为它将replace视为关键字而不是 function 名称。 Maybe a bug?也许是一个错误?

Some quick experiments indicate that a possible solution is to surround replace in backticks to mark is as a name (see keyword link above).一些快速实验表明,一种可能的解决方案是在反引号中包围replace以将其标记为名称(参见上面的关键字链接)。 Please give it a try:请试一试:

@Query("update bookmarks set bookmarks_path= `replace`(bookmarks_path,:oldPath,:newPath) where bookmarks_path like :oldPath || '%'")
fun updateBookmarksPath(oldPath: String, newPath: String): Completable

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM