房间数据库获取集合类型
Room database get Set of type
我需要从 RoomDatabase
中获取 set
个字符串
实体:
@Entity
data class Data(
@PrimaryKey val id: Int,
val type: String,
val photo: String
)
我需要获取 set
类型。我尝试了以下方法:
...
@Query("SELECT type FROM data")
fun getAllTypes(): LiveData<Set<String>>
...
现在我有下一个错误:
C:\...\storage\DataDao.java:24: error: Not sure how to convert a Cursor to this method's return type (androidx.lifecycle.LiveData<java.util.Set<java.lang.String>>).
public abstract androidx.lifecycle.LiveData<java.util.Set<java.lang.String>> getAllTypes();
[WARN] Incremental annotation processing requested, but support is disabled because the following processors are not incremental: androidx.room.RoomProcessor (DYNAMIC).
> Task :app:kaptDebugKotlin FAILED
也许是这样:
@Query("SELECT type FROM data")
必须是这样的:
@Query("SELECT type FROM Data")
抱歉,您无法获取 Set。根据 SELECT 查询的文档,您只能使用列表或数组。
https://developer.android.com/reference/androidx/room/Query
如果您需要独特的物品,您可以使用 SQL 的 DISTINCT。
我需要从 RoomDatabase
set
个字符串
实体:
@Entity
data class Data(
@PrimaryKey val id: Int,
val type: String,
val photo: String
)
我需要获取 set
类型。我尝试了以下方法:
...
@Query("SELECT type FROM data")
fun getAllTypes(): LiveData<Set<String>>
...
现在我有下一个错误:
C:\...\storage\DataDao.java:24: error: Not sure how to convert a Cursor to this method's return type (androidx.lifecycle.LiveData<java.util.Set<java.lang.String>>).
public abstract androidx.lifecycle.LiveData<java.util.Set<java.lang.String>> getAllTypes();
[WARN] Incremental annotation processing requested, but support is disabled because the following processors are not incremental: androidx.room.RoomProcessor (DYNAMIC).
> Task :app:kaptDebugKotlin FAILED
也许是这样:
@Query("SELECT type FROM data")
必须是这样的:
@Query("SELECT type FROM Data")
抱歉,您无法获取 Set。根据 SELECT 查询的文档,您只能使用列表或数组。
https://developer.android.com/reference/androidx/room/Query
如果您需要独特的物品,您可以使用 SQL 的 DISTINCT。