Reputation: 1165
I am developing simple web application with play framework with scala and storing databse in mysql and i am getting error
what am i doing :
models/Keyword.scala
case class Keyword(blog: String,cat: String,word: String,count: Long, summaryId: String)
object Keyword {
val keyw = {
get[String]("keyword.blog")~
get[String]("keyword.cat")~
get[String]("keyword.word")~
get[Long]("keyword.count") ~
get[String]("keyword.summaryId") map {
case blog~cat~word~count~summaryId => Keyword(blog,cat,word,count, summaryId)
}
}
def all(): List[Keyword] = DB.withConnection { implicit c =>
SQL("select * from keyword").as(Keyword.keyw *)
}
def create(key: Keyword){DB.withConnection{implicit c=>
SQL("insert into keyword values({blog}, {cat}, {word},{count},{summaryId})").on('blog->key.blog,
'cat -> key.cat,
'word-> key.word,
'count-> key.count,
'summaryId -> key.summaryId).executeUpdate()
}
}
controllers/Application.scala
object Application extends Controller {
val ta:Form[Keyword] = Form(
mapping(
"blog" -> nonEmptyText,
"cat" -> nonEmptyText,
"word" -> nonEmptyText,
"count"-> of[Long],
"summaryId"-> nonEmptyText
)(Keyword.apply)(Keyword.unapply)
)
def index = Action {
Ok(html.index(ta));
}
def newTask= Action { implicit request =>
ta.bindFromRequest.fold(
errors => BadRequest(html.index(errors)),
keywo => {
Keyword.create(keywo)
Ok(views.html.data(Keyword.all()))
}
)
}
- when i am using h2-database that time it is running
- when i am using MySQL that time it is giving exception
Give me some idea to solve this issue!
Upvotes: 2
Views: 1498
Reputation: 1185
I would often get this error when trying to do an INSERT with pgsql and anorm. I sovled it by using the Sql String parser. this may work for you... not 100% sure though:
def all(): List[Keyword] = DB.withConnection { implicit c =>
SQL("select * from keyword").as(Keyword.keyw *).execute(SqlParser.scalar[String].singleOpt)
}
Upvotes: 0
Reputation: 344
Create an AnormExtension helper in app/helpers folder. Call it AnormExtension.scala. Code below:
object AnormExtension {
implicit def rowJavaLongToLong: Column[Long] = Column.nonNull { (value, meta) =>
val MetaDataItem(qualified, nullable, clazz) = meta
value match {
case d: java.lang.Long => Right(d.longValue())
case _ => Left(TypeDoesNotMatch("Cannot convert " + value + ":" + value.asInstanceOf[AnyRef].getClass + " to Long for column " + qualified))
}
}
}
Upvotes: 1