如何使用 Scala Anorm 编写 select 查询?
How to write a select query with Scala Anorm?
如何使用 Scala Anorm 获取 user_name
值?我试过了:
val result = SQL("SELECT user_name a FROM user WHERE user_id = {user_id}").on('user_id -> 1).executeQuery()
println("result ="+result)
而且,我得到的是 result = SqlQueryResult(FlattenedManagedResource[?](...),false)
而不是我期望 table 中的 user_name
值。
我应该怎么做才能得到我想要的?
你必须使用结果解析器;例如
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
如何使用 Scala Anorm 获取 user_name
值?我试过了:
val result = SQL("SELECT user_name a FROM user WHERE user_id = {user_id}").on('user_id -> 1).executeQuery()
println("result ="+result)
而且,我得到的是 result = SqlQueryResult(FlattenedManagedResource[?](...),false)
而不是我期望 table 中的 user_name
值。
我应该怎么做才能得到我想要的?
你必须使用结果解析器;例如
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