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

lim*_*xin 2 android android-room

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

Run Code Online (Sandbox Code Playgroud)

我正在使用房间数据库,这是我的代码

运行时发现这段代码的格式不正确,android studio提示我:Expression Expected, Got 'replace'

像这样

在此输入图像描述

我想要达到这样的效果

----路径------------------路径

/根/1/1--------------/堆栈/1/1

/根/名称/1------/堆栈/名称/1

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

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

/根/js/1------------/stack/js/1

Bob*_*der 5

REPLACE既是SQLite 关键字,又是字符串函数的名称:replace()(根据您的预期用途) 。Room 注释处理器似乎拒绝您的查询字符串,因为它被视为replace关键字而不是函数名称。也许是一个错误?

一些快速实验表明,一个可能的解决方案是用replace反引号括起来以将 is 标记为名称(请参阅上面的关键字链接)。请试一试:

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