limengxin
limengxin

Reputation: 574

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'

like this

enter image description here

I want to achieve the effect like this

----path------------------path

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

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

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

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

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

please

Upvotes: 2

Views: 1464

Answers (1)

Bob Snyder
Bob Snyder

Reputation: 38299

REPLACE is both an SQLite keyword and, in the case of your intended use, the name of a string function: replace() . The Room annotation processor appears to be rejecting your query string because it is treating replace as keyword instead of function name. 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). 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

Upvotes: 5

Related Questions