I need to perform column sum operation
But I don't understand what exactly I need to insert into fun getSumItem(): Flow<Cursor>
this is my Dao
@Dao
interface Dao {
@Insert
fun InsertItem(item: Income)
@Query("SELECT SUM(sum) AS value FROM income")
fun getSumItem(): Flow<Cursor>
}
this is my table
@Entity(tableName = "income")
data class Income(
@PrimaryKey(autoGenerate = true)
var id: Int? = null,
@ColumnInfo(name = "date")
var date: String,
@ColumnInfo(name = "type")
var type: String,
@ColumnInfo(name = "sum")
var sum: Float,
)
I did not find a similar example and any detailed information on the Internet.
when I use Cursor
i get this error:
Entities and POJOs must have a usable public constructor. You can have an empty constructor or a constructor whose parameters match the fields (by name and type). - android.database.Cursor
all other options that I found on the Internet also gave errors
CodePudding user response:
When you say SELECT SUM(sum) AS value FROM income
the output that Room handles will be a Cursor with a single column named value
. Room does not know how to extract this value and build a Cursor object from that single value.
Room expects to extract data from the underlying Cursor that is generated by the SQLite API, which Room is a wrapper to/around, and deliver the data according to the object that the function returns (and thus must be able to ascertain how to build the resultant object).
As Room extracts a single value then you simply need to retrieve the value returned as a Float.
fun getSumItem(): Flow<Float>