i have tried result set mysql database table using anorm. here code.
package models import play.api.db._ import play.api.play.current import scala.collection.mutable._ import anorm._ import anorm.sqlparser._ case class brand(id: int, name: string) object brand { /** * parse brand resultset */ val simple = { get[int]("m_brand.idbrand") ~ get[string]("m_brand.brandname") map { case id~name => brand(id, name) } } /** * construct map[string,string] needed fill select options set. */ def options: seq[(string,string)] = db.withconnection { implicit connection => sql("select * m_brand order brandname").as(brand.simple *). foldleft[seq[(string, string)]](nil) { (cs, c) => c.id.fold(cs) { id => cs :+ (id.tostring -> c.name) } } } }
i tried change code experiments not worked.
but got error
read stdout: d:\projects\test\project_vendorm8\app\models\brand.scala:69: type mismatch; read stdout: found : scala.collection.immutable.nil.type read stdout: required: scala.collection.mutable.seq[(string, string)] d:\projects\test\project_vendorm8\app\models\brand.scala:69: type mismatch; found : scala.collection.immutable.nil.type required: scala.collection.mutable.seq[(string, string)] read stdout: foldleftseq[(string, string)] { (cs, c) => foldleftseq[(string, string)] { (cs, c) => read stdout: ^
as asked in comments, simpler solution not use map , write:
def options: seq[(string,string)] = db.withconnection { implicit connection => sql("select * m_brand order brandname").as(simple *) .map( b => (b.id.tostring, b.name)) .toseq }
Comments
Post a Comment