Reputation: 169
i try to do the following:
@Query("SELECT COUNT(s_row) FROM table WHERE data = :data")
fun isDataExist(data: String): Boolean{
if (*QUERY_RETURN*) > 0{
return true
} else {
return false
}
}
so my question is is there a way to have a body to test the query return and then return a boolean if the data already exist in the database?
Upvotes: 0
Views: 618
Reputation: 8152
For the starters dao
is interface
and you should not implement its methods.
@Dao
interface DataDao {
//dao function should not have a body
@Query("SELECT * FROM some_table")
suspend fun selectFunction():List<String>
//dao function should not have a body
@Query("DELETE FROM some_table")
suspend fun clearData() .... }
You just write the queries, ROOM has underlying logic to do the rest.
In your case what you can do is to use an identifier or an id to query if the item exists in the database.
You can do something like this - note this is a suspend
function as this is a database operation.
@Query("SELECT EXISTS(SELECT 1 FROM table_name WHERE id=:id)")
suspend fun isDataExist(id: String): Boolean
Afterwards you can use this function in a repository implementation or ViewModel (using ViemodelScope)
Upvotes: 1