如何从MySQL到ROOM实现SUM(),DAY(),MONTH()和YEAR()

时间:2018-04-21 15:48:52

标签: android kotlin android-room

我在SQL中有这个代码:

SELECT SUM(number), DAY(date) FROM conting GROUP BY DAY(date) ORDER BY DAY(date) ASC

然后我需要在房间里转换:

@Query("SELECT SUM(number), DAY(date) FROM Records GROUP BY DAY(date) ORDER BY DAY(date) ASC") fun getResumeData(): List<Graph>

Graph.kt

data class Graph (
    @ColumnInfo(name = "DAY(date)") var index: Int,
    @ColumnInfo(name = "SUM(number)") var value: Int
)

但它返回错误no such function: DAY我该如何解决? 注意:

this is my sql database

this is my sql query result

1 个答案:

答案 0 :(得分:1)

Android(和Room)使用SQLite,而不是MySQL,因此您需要使用SQLite可用的功能构建查询。您可以使用SQLite的date()datetime()函数执行类似操作,有关详细信息,请参阅https://www.sqlite.org/lang_datefunc.html