简体   繁体   中英

How to write a select query with Scala Anorm?

How to get user_name value , using Scala Anorm? I tried:

 val result = SQL("SELECT user_name a FROM user WHERE user_id = {user_id}").on('user_id -> 1).executeQuery()           
 println("result ="+result)

And, I am getting result = SqlQueryResult(FlattenedManagedResource[?](...),false) instead of this I was expecting user_name value in table.

How should I do to get what I want?

You have to use a result parser; eg

import anorm.SqlParser.scalar
val parser = scalar[String].single
// expect only one row (single),
// with only one column (scalar),
// of type String

val user: String = SQL("SELECT user_name a FROM user WHERE user_id = {user_id}").on('user_id -> 1).as(parser)

It's recommended you have a look at the documentation

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