I'm trying to connect to a mysql database with slick 1.0.0.
What I've done so far:
in Build.scala I've added
val appDependencies = Seq(
anorm,
"mysql" % "mysql-connector-java" % "5.1.24",
"com.typesafe.slick" % "slick_2.10" % "1.0.0",
"org.slf4j" % "slf4j-nop" % "1.6.4"
)
in application.conf
db.default.driver=com.mysql.jdbc.Driver
db.default.url="url to mysql db"
db.default.user=user
db.default.pass=password
and now I'm trying to read an Entry from the DB. For this I have a model
package models
import scala.slick.driver.MySQLDriver.simple._
import Database.threadLocalSession
object Organisations extends Table[(Int, String)]("Organisation")
{
def id = column[Int]("id", O.PrimaryKey)
def name = column[String]("name")
def * = id ~ name
}
and now I'd like to just output the entries
val orgs =
for { o <- Organisations } yield o.name
println("Length" + orgs.toString())
But it doesn't work. I'm sure I've made plenty of errors, but there don't seem to be andy slick tutorials with mysql.
Thank you for your patience and I hope my explanations are clear.
Using Slick requires a bit of boilerplate, creating session and all that, checkout the Play-Slick plugin written by Fredrik Ekholdt (typesafe)!
It does all that plumbing for you and there are good examples on the wiki on how to use it.
https://github.com/freekh/play-slick/
The new Slick 2.0 also features a Code Generator that can be used together with Play Framework evolutions.
This means you don't have to write the boilerplate for Slick anymore. Just write your database changes using evolutions files, and immediately access the new tables from your code.
You can find a complete example using MySQL here:
https://github.com/papauschek/play-slick-evolutions
And more information on how it works:
http://blog.papauschek.com/2013/12/slick-2-0-code-generator-play-framework-evolutions/
The Play team have also been working on a slick benchmark for Techempower. It is a work in progress but we'll shortly be raising a PR on the completed version (next 24 hours I suspect):
https://github.com/nraychaudhuri/FrameworkBenchmarks/tree/adding_missing_slickness/play-slick
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With