Error:Not sure how to convert a Cursor to this method\'s return type
Error:Execution failed for task \':app:compileDebugJavaWithJavac\'.
Compilation failed; see the
Make sure if there is
There is a problem with the query: [SQLITE_ERROR] SQL error or missing database (no such table: METRO)
error in your build log before the error you mentioned in your description. If it is, you may have forgotten to add your new entity Pojo to database class. something like this
@Database(entities = {Table.class,ForgottenTable.class}, version = 1)
public abstract class Database extends RoomDatabase {
//class codes
}
For me, I was using wrong return type for queries.
For my case, after got "Not sure how to convert a Cursor to this method's return type”:
delete the "build" and re-build, the error disappear.
Recently I've had the same problem but I was using Coroutines
within the Dao
function, e.g.:
@Query("SELECT * FROM Dummy")
suspend fun get(): LiveData<List<Dummy>>
And was unable to compile, but after removing the suspend
everything worked just fine. It's not needed when returning LiveData
. suspend
and LiveData
seem not to work together (as of now).
I got this error when I was trying do some aggregate functions in the query, like sum and count and then using aliases in the column names.
select count(users.id) as userCount, ...
It so happens that the alias name like userCount
above, must match the field name in the model.
I've got a different use case for my apps.
So, I'm trying to return the actual Cursor
type.
E.g:
@Query("SELECT * FROM tbl_favourite")
abstract suspend fun selectAll(): Cursor
The above code will always throw Error:Not sure how to convert a android.database.Cursor to this method's return type
But as I recall correctly, the official docs also stated here that Room supports Cursor
.
After trying to debug the error log, and open up the MyTableDao_Impl.java
file I've found that looks like Cursor
are having an unhealthy relationship with suspend
keywords.
Thus, I've corrected my code to be like this:
@Query("SELECT * FROM tbl_favourite")
abstract fun selectAll(): Cursor
And voila, it works.