2016-04-26 3 views
1

ように私はこのようなコードを持っている:Anorm GETのInt結果

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) 
} 

それは結果としてStringなります。

どうすれば結果をIntとすることができますか?

答えて

2

Anorm documentationを見ると、行パーサーの使用方法がわかります。

Intについては、SqlParser.get[Int]またはその都合の別名SqlParser.intを使用してください。

ここでは、1列の行を探していますが、.scalarが良いです。

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

注(括弧なし)Anorm補間SQL"..."

0

iは、その動作するはずだと思う

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 
}