I have tried to get result set from mySQL database table using anorm. here is my 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 a Brand from a ResultSet
*/
val simple = {
get[Int]("m_brand.idbrand") ~
get[String]("m_brand.brandName") map {
case id~name => Brand(id, name)
}
}
/**
* Construct the Map[String,String] needed to fill a select options set.
*/
def options: Seq[(String,String)] = DB.withConnection { implicit connection =>
SQL("select * from m_brand order by 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 with some experiments but not worked.
But I got this error
Read from stdout: D:\\PROJECTS\\test\\Project_VendorM8\\app\\models\\Brand.scala:69: type mismatch; Read from stdout: found : scala.collection.immutable.Nil.type Read from 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 from stdout: foldLeftSeq[(String, String)] { (cs, c) => foldLeftSeq[(String, String)] { (cs, c) => Read from stdout: ^
As asked in the comments, would a simpler solution not be just to use map and write:
def options: Seq[(String,String)] = DB.withConnection { implicit connection =>
SQL("select * from m_brand order by brandName").as(simple *)
.map( b => (b.id.toString, b.name))
.toSeq
}
change
foldLeft[Seq[(String, String)]](Nil)
to
foldLeft(Seq.empty[(String, String)])
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.