daily pastebin goal
49%
SHARE
TWEET

Untitled

a guest Apr 21st, 2018 57 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import slick.driver.MySQLDriver
  2. import slick.lifted.ProvenShape
  3.  
  4. import scala.concurrent.Await
  5. import scala.concurrent.duration.Duration
  6.  
  7. import scala.concurrent.ExecutionContext.Implicits.global
  8.  
  9. object SlickMySQLInsert extends MySQLDriver {
  10.  
  11.   import api._
  12.  
  13.   case class Coffee(id: Option[Int], name: String, price: Double)
  14.   class Coffees(tag: Tag) extends Table[Coffee](tag, "COFFEES") {
  15.     def id: Rep[Int] = column[Int]("ID", O.AutoInc, O.PrimaryKey)
  16.     def name: Rep[String] = column[String]("COF_NAME")
  17.     def price: Rep[Double] = column[Double]("PRICE")
  18.     def * : ProvenShape[Coffee] = (id.?, name, price) <> (Coffee.tupled, Coffee.unapply)
  19.   }
  20.   val coffees = TableQuery[Coffees]
  21.  
  22.  
  23.   def main(args: Array[String]): Unit = {
  24.  
  25.     val insert = sqlu"insert into `COFFEES` (`COF_NAME`,`PRICE`)  values ('test',42.0)"
  26.     val getId = sql"SELECT LAST_INSERT_ID()".as[Int].head
  27.  
  28.     val io = (for {
  29.       c <- insert
  30.       id <- getId if c == 1
  31.     } yield id).withPinnedSession
  32.  
  33.  
  34.     val db = Database.forDriver(new com.mysql.jdbc.Driver, "jdbc:mysql://localhost/coffee?serverTimezone=UTC", "root")
  35.  
  36.     val res = Await.result(db.run(io), Duration.Inf)
  37.     println(res)
  38.   }
  39. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top