如何将OrdercBy param传递给Room @Query?

时间:2017-12-31 17:37:28

标签: android android-room

我在Dao中有这样的事情:

@Query("select * from orders ORDER BY time desc")
fun getAllOrders(): DataSource.Factory<Int, Order>

但我想做这样的事情:

@Query("select * from orders ORDER BY :order :orderDirection")
fun getAllOrders(order:String, orderDirection:String): DataSource.Factory<Int, Order>

知道如何实现这一目标吗?编译说:

  

错误:(9,1)错误:无关输入&#39;:orderDirection&#39;期待{,&#39 ;;&#39;,&#39;,&#39;,&#39; =&#39;,&#39; *&#39;,&#39; +&# 39;,&#39; - &#39;,&#39; ||&#39;,&#39; /&#39;,&#39;%&#39;,&#39;&lt; &lt;&#;;&#39;&gt;&gt;&#39;,&#39;&amp;&#39;,&#39; |&#39;,&#39;&lt;&#39 ;,&#39;&lt; =&#39;,&#39;&gt;&#39;,&#39;&gt; =&#39;,&#39; ==&#39;,&# 39;!=&#39;,&#39;&lt;&gt;&#39;,K_ALTER,K_ANALYZE,K_AND,K_ASC,K_ATTACH,K_BEGIN,K_BETWEEN,K_COLLATE,K_COMMIT,K_CREATE,K_DELETE,K_DESC,K_DETACH,K_DROP, K_END,K_EXPLAIN,K_GLOB,K_IN,K_INSERT,K_IS,K_ISNULL,K_LIKE,K_LIMIT,K_MATCH,K_NOT,K_NOTNULL,K_OR,K_PRAGMA,K_REGEXP,K_REINDEX,K_RELEASE,K_REPLACE,K_ROLLBACK,K_SAVEPOINT,K_SELECT,K_UPDATE,K_VACUUM,K_VALUES,K_WITH, UNEXPECTED_CHAR}

我也试过这个:(传递&#39;时间desc&#39;作为参数)但是Room忽略了它。

@Query("select * from orders ORDER BY :order")
fun getAllOrders(order:String): DataSource.Factory<Int, Order>

有什么想法吗?

1 个答案:

答案 0 :(得分:1)

这是您的SQL查询中的语法错误。

请确保在形成查询时,在每行末尾添加一个空格。

尝试使用这种类型的查询来传递参数

@Query("SELECT * FROM " + "UGC"
            + " WHERE "
            + UGC_USER_ID + "=" + ":userID"
            + " AND "
            + UGC_ID + "=" + ":id"
            + ";")
          fun getUgcData(userID:Integer,id:Integer):List<Ugc>