简体   繁体   中英

Scala Anorm select 2 values using column resolution

According to documentation ( https://www.playframework.com/documentation/2.5.x/Anorm ), I can do the following to retrieve values from 2 columns:

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

This does not compile...

Causes this:

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

I'm just looking for a single method of doing this (for anorm 2.5+). I was using regular parsers but am looking for this more concise way of doing it.

The code is not complete: to get a single result as such tuple, the .single combinator must be used.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM