Room “Not sure how to convert a Cursor to this method's return type”: which method?

后端 未结 20 1374
遇见更好的自我
遇见更好的自我 2020-12-14 14:15
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          


        
相关标签:
20条回答
  • 2020-12-14 15:01

    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
    }
    
    0 讨论(0)
  • 2020-12-14 15:02

    For me, I was using wrong return type for queries.

    0 讨论(0)
  • 2020-12-14 15:03

    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.

    0 讨论(0)
  • 2020-12-14 15:06

    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).

    0 讨论(0)
  • 2020-12-14 15:06

    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.

    0 讨论(0)
  • 2020-12-14 15:09

    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.

    0 讨论(0)
提交回复
热议问题