0

I have an EditText on fragment where I want to enter a query directly, without a pre-installed Query annotation for the function. I wrote this function in Dao:

@Query(":query")
fun getList(query: String): Flow<List<String>>

in my ViewModel:

getList("SELECT * FROM my_table").collect {
    // retrieve data
}

However, this method throws an error:

error: Must have exactly 1 query in the value of @Query or @DatabaseView

Is it even possible to write queries to the ROOM database directly through UI?

onesector
  • 361
  • 5
  • 18

1 Answers1

0

First of all, providing users access to your database is wrong. One wrong query can wipe off important data.

That being said, the error is because room compiles the queries beforehand for the verification of SQL queries. While performing compilation it throws this error. So I think fully dynamic queries are not possible in room.

But you can try this hack.

Mr. Techie
  • 622
  • 7
  • 17