简体   繁体   English

Anorm结果为Int

[英]Anorm get Int as result

I have a code like this: 我有这样的代码:

def getUserId(email: String) = DB.withConnection { implicit con =>
  SQL("select userId from signUp where email = {email}").
    on('email -> email).as(SqlParser.str("access_token").singleOpt)
}

It gives String as a result. 结果是String

How can I get the result as Int . 我怎样才能得到结果为Int

If you have a look at the Anorm documentation , you can see how to use the row parsers. 如果您查看Anorm文档 ,则可以看到如何使用行解析器。

For Int , either use SqlParser.get[Int] , or its convenient alias SqlParser.int . 对于Int ,请使用SqlParser.get[Int]或其方便的别名SqlParser.int

There you are looking for a row with a single column, .scalar is better. 在那里,您正在寻找单列的行, .scalar更好。

val res: Option[Int] = 
  SQL"SELECT userId FROM signUp WHERE email = $email".
    as(SqlParser.scalar[Int].singleOpt)

Note the Anorm interpolation SQL"..." (without the parenthesis). 注意Anorm插值SQL"..." (不带括号)。

i think its should work 我认为它应该工作

def getUserId(email: String) = { 
    val userId = DB.withConnection { 
       implicit a => SQL( """ select userId from signUp where email = {email} """).on('email -> email).as(SqlParser.str("access_token").singleOpt) 
    } 
    userId.toInt 
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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