[英]Android Room Kotlin throws Delete Query error
我正在尝试使用 Android Room 2.3.0,我目前收到以下编译错误:
项目:
error: Not sure how to handle query method's return type (java.lang.Object). DELETE query methods must either return void or int (the number of deleted rows).
public abstract java.lang.Object deleteAllProjects(@org.jetbrains.annotations.NotNull()
error: Query method parameters should either be a type that can be converted into a database column or a List / Array that contains such type. You can consider adding a Type Adapter for this.
kotlin.coroutines.Continuation<? super kotlin.Unit> continuation);
error: Unused parameter: continuation
public abstract java.lang.Object deleteAllProjects(@org.jetbrains.annotations.NotNull()
error: Type of the parameter must be a class annotated with @Entity or a collection/array of it.
kotlin.coroutines.Continuation<? super java.lang.Long> continuation);
error: Not sure how to handle insert method's return type.
public abstract java.lang.Object insertProject(@org.jetbrains.annotations.NotNull()
error: Not sure how to handle delete method's return type. Currently the supported return types are void, int or Int.
public abstract java.lang.Object deleteProject(@org.jetbrains.annotations.NotNull()
error: Type of the parameter must be a class annotated with @Entity or a collection/array of it.
kotlin.coroutines.Continuation<? super kotlin.Unit> continuation);
反道:
Execution failed for task ':app:kaptDebugKotlin'.
> A failure occurred while executing org.jetbrains.kotlin.gradle.internal.KaptWithoutKotlincTask$KaptExecutionWorkAction
> java.lang.reflect.InvocationTargetException (no error message)
但是,我的 ProjectDao.kt 文件具有以下内容:
@Dao
interface ProjectDao {
@Query("SELECT * FROM table_projects")
fun getAll(): List<Project>
@Insert
suspend fun insertProject(project: Project): Long
@Insert
fun insertProjects(projects: List<Project>)
@Delete
suspend fun deleteProject(project: Project)
@Query("DELETE FROM table_projects")
suspend fun deleteAllProjects()
@Transaction
@Query("SELECT * FROM table_projects")
fun getAllProjectsWithCounters(): List<ProjectWithCounters>
@Transaction
@Query("SELECT * FROM table_projects WHERE id_project=:projectID")
fun getProjectWithCounters(projectID: Long): ProjectWithCounters
}
我以前没有遇到过任何问题,突然间我收到了这些错误,我不知道是什么原因造成的。
谢谢!
我解决了这个问题如下
我将 Kotlin 版本设置为1.6.10而不是 Room 设置为2.4.2
使用@Delete
注释,您必须定义一个返回数据类型:
DELETE 查询方法必须返回 void 或 int(删除的行数)。
所以这应该是:
@Delete
suspend fun deleteProject(project: Project): Integer
成功时返回1
,数据库中不存在project
时返回0
。
设置 kotlin 版本为 1.5.21 或 1.5.31 Kotlin 1.6.0 不能在 ROOM QUERY 中使用暂停。
classpath 'org.jetbrains.kotlin:kotlin-gradle-plugin:1.5.21'
在 Dao 函数中使用协程时我遇到了同样的问题
对我来说只是改变
kapt "androidx.room:room-compiler:$room_version"
至
annotationProcessor "androidx.room:room-compiler:$room_version"
如果您使用List or MutableLiveData
检查此答案
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.