Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import slick.driver.MySQLDriver
- import slick.lifted.ProvenShape
- import scala.concurrent.Await
- import scala.concurrent.duration.Duration
- import scala.concurrent.ExecutionContext.Implicits.global
- object SlickMySQLInsert extends MySQLDriver {
- import api._
- case class Coffee(id: Option[Int], name: String, price: Double)
- class Coffees(tag: Tag) extends Table[Coffee](tag, "COFFEES") {
- def id: Rep[Int] = column[Int]("ID", O.AutoInc, O.PrimaryKey)
- def name: Rep[String] = column[String]("COF_NAME")
- def price: Rep[Double] = column[Double]("PRICE")
- def * : ProvenShape[Coffee] = (id.?, name, price) <> (Coffee.tupled, Coffee.unapply)
- }
- val coffees = TableQuery[Coffees]
- def main(args: Array[String]): Unit = {
- val insert = sqlu"insert into `COFFEES` (`COF_NAME`,`PRICE`) values ('test',42.0)"
- val getId = sql"SELECT LAST_INSERT_ID()".as[Int].head
- val io = (for {
- c <- insert
- id <- getId if c == 1
- } yield id).withPinnedSession
- val db = Database.forDriver(new com.mysql.jdbc.Driver, "jdbc:mysql://localhost/coffee?serverTimezone=UTC", "root")
- val res = Await.result(db.run(io), Duration.Inf)
- println(res)
- }
- }
Add Comment
Please, Sign In to add comment