Scala Anorm select 2 个值使用列解析

Scala Anorm select 2 values using column resolution

根据文档 (https://www.playframework.com/documentation/2.5.x/Anorm),我可以执行以下操作以从 2 列中检索值:

val res: (String, Int) = SQL"SELECT text, count AS i".map(row =>
  row[String]("text") -> row[Int]("i")
)

这不编译...

原因:

Expression of type SimpleSql[(String, Int)] doesn't conform to expected type (String, Int)

我只是在寻找一种执行此操作的方法(对于 anorm 2.5+)。我使用的是常规解析器,但正在寻找这种更简洁的方法。

代码不完整:要获得单个结果作为这样的元组,必须使用 .single 组合器。

val res: (String, Int) = SQL"SELECT text, count AS i".map(row =>
  row[String]("text") -> row[Int]("i")
).single

Using Anorm flatteners is easier for tuple result: see examples