我正在使用android jetpack进行android编程,需要检索某些列的计数,问题是我不知道如何将COUNT(column)的列映射到房间实体中,我是我收到此错误:
Caused by: java.lang.IllegalArgumentException: column '`COUNT(Deuda.id)`' does not exist. Available columns: [COUNT(Deuda.id), COUNT(pagado)]
at android.database.AbstractCursor.getColumnIndexOrThrow(AbstractCursor.java:340)
at androidx.room.util.CursorUtil.getColumnIndexOrThrow(CursorUtil.java:108)
at room.components.daos.DeudaDAO_Impl$13.call(DeudaDAO_Impl.java:568)
at room.components.daos.DeudaDAO_Impl$13.call(DeudaDAO_Impl.java:563)
at androidx.room.RoomTrackingLiveData$1.run(RoomTrackingLiveData.java:90)
这是我的查询:
@Query("SELECT COUNT(Deuda.id), COUNT(pagado) FROM Deuda GROUP BY tipo")
fun getCuentaTiposDeuda() : LiveData<List<CantidadTipoDeuda>>
这是我要执行查询的实体:
@Entity(foreignKeys = arrayOf(
ForeignKey(entity = Cuenta::class,
parentColumns = arrayOf("id"),
childColumns = arrayOf("cuenta_id"),
onDelete = ForeignKey.CASCADE)
))
class Deuda(@PrimaryKey(autoGenerate = true) var id: Int,
var titulo: String? = null,
var tipo: Int = 0,
var monto: Float = 0f,
var nota: String? = null,
var fecha_adquision: String? = null,
var pagado: Float = 0f,
var estado: Int? = null,
var cuenta_id: Int? = null
)
这是我要存储结果的房间实体:
import androidx.room.ColumnInfo
data class CantidadTipoDeuda (
@ColumnInfo(name = "COUNT(Deuda.id)") val tipoDeuda: Float?,
@ColumnInfo(name = "COUNT(pagado)") val cantidadPorTipo: Float?
)
那么,如何使用房间处理COUNT个?