错误: Not sure how to handle query method‘s return type (long). UPDATE query methods must either re...

room_version=2.4.2 会报一下错误

下面是我的写的demo 中Dao部分
@Dao
interface CarBrandDao {
    // TODO 这里有BUG 返回suspend就会报错
    @Query("DELETE FROM CarBrandEntity")
    suspend fun clearCarBrand()

    @Insert(onConflict = OnConflictStrategy.REPLACE)
    suspend fun insertCarBrand(carBrandList: List<CarBrandEntity>)

    @Query("SELECT * FROM CarBrandEntity")
    fun getCarBrand():PagingSource<Int,CarBrandEntity>
}
在网上找了好久,才找到对应的解决方案,深渊巨坑,在之前的一个demo 中也遇到类似的问题,不过没有去处理,现在终于决绝。

解决方式一:

If you using kotlin version (1.7.0) should work with room latest alpha version (2.5.0-alpha02)

classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:1.7.0"

implementation "androidx.room:room-runtime:2.5.0-alpha02"
implementation "androidx.room:room-ktx:2.5.0-alpha02"
kapt "androidx.room:room-compiler:2.5.0-alpha02"

解决方式二:
If you want using room in stable version (2.4.2) should work with kotlin version (1.6.20)

classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:1.6.20"

implementation "androidx.room:room-runtime:2.4.2"
implementation "androidx.room:room-ktx:2.4.2"
kapt "androidx.room:room-compiler:2.4.2"
I have tried both and they work. this is the reference 

文章来源 原文---stackoverflowhttps://stackoverflow.com/questions/60050991/how-to-use-suspend-function-on-room-db

一下是对应的issues 

issueshttps://issuetracker.google.com/issues/236612358
  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
It seems like you are trying to return a List of ImageInfo objects from the method `getALLUsers()` in the `SecondActivity` class. However, you are getting an error related to converting a Cursor object to the return type. To fix this error, you need to fetch the data from the Cursor object and convert it into a List of ImageInfo objects. Here's an example of how you can do this: ``` public List<ImageInfo> getALLUsers() { SQLiteDatabase db = this.getReadableDatabase(); List<ImageInfo> imageInfoList = new ArrayList<>(); String[] projection = {...}; // Define the columns you want to fetch String sortOrder = "..."; // Define the sort order of the results Cursor cursor = db.query( TABLE_NAME, projection, null, null, null, null, sortOrder ); while (cursor.moveToNext()) { // Extract the data from the cursor and create a new ImageInfo object ImageInfo imageInfo = new ImageInfo(); imageInfo.setId(cursor.getInt(cursor.getColumnIndexOrThrow(COLUMN_ID))); imageInfo.setName(cursor.getString(cursor.getColumnIndexOrThrow(COLUMN_NAME))); imageInfo.setPath(cursor.getString(cursor.getColumnIndexOrThrow(COLUMN_PATH))); // Add the ImageInfo object to the list imageInfoList.add(imageInfo); } cursor.close(); return imageInfoList; } ``` In this example, we are using the `query()` method to fetch all rows from the database table and store them in a Cursor object. Then, we iterate over the cursor using a while loop and extract the data from each row to create a new ImageInfo object. Finally, we add the ImageInfo object to a List and return the list.
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值