使用光滑插入MySQL

时间:2015-11-29 21:35:43

标签: mysql scala slick

我有一张名为" KLIJENTI"在ampps localhost服务器上的MySQL数据库中。表由4列组成:ID_KLIJENTA - int,IME_KLIJENTA - 字符串,PREZIME_KLIJENTA - 字符串和ADRESA_KLIJENTA - 字符串。 我按照关于如何使用光滑插入数据库和从数据库读取的教程,但它没有做我想要的。

这是我的application.conf:

scalaTest = {
url = "jdbc:mysql://localhost/Scala1"
user = "root"
password = "mysql"
driver = com.mysql.jdbc.Driver
connectionPool = disabled
keepAliveConnection = true
logStatements = true
}

build.sbt:

name := """project4"""

mainClass in Compile := Some("HelloSlick")

libraryDependencies ++= List(
  "com.typesafe.slick" %% "slick" % "3.1.0-RC2",
  "org.slf4j" % "slf4j-nop" % "1.7.10",
  "com.h2database" % "h2" % "1.4.187",
  "org.scalatest" %% "scalatest" % "2.2.4" % "test" ,
  "mysql" % "mysql-connector-java" % "5.1.28"
)

fork in run := true

这是我的helloSlick.scala:

import scala.concurrent.{Future, Await}
import scala.concurrent.ExecutionContext.Implicits.global
import scala.concurrent.duration.Duration
import slick.backend.DatabasePublisher
import slick.driver.MySQLDriver.api._


case class Klijent(ID_KLIJENTA: Option[Int] = None, IME_KLIJENTA: String, PREZIME_KLIJENTA: String, ADRESA_KLIJENTA: String)

class Klijenti(tag: Tag) extends Table[Klijent](tag, "KLIJENTI") {
  def ID_KLIJENTA = column[Option[Int]]("ID_KLIJENTA", O.PrimaryKey, O.AutoInc)
  def IME_KLIJENTA = column[String]("IME_KLIJENTA")
  def PREZIME_KLIJENTA = column[String]("PREZIME_KLIJENTA")
  def ADRESA_KLIJENTA = column[String]("ADRESA_KLIJENTA")

  def * = (ID_KLIJENTA, IME_KLIJENTA, PREZIME_KLIJENTA , ADRESA_KLIJENTA) <>((Klijent.apply _).tupled, Klijent.unapply)
}

// The main application
object HelloSlick extends App {

  val db = Database.forConfig("scalaTest")
  val mojiKlijenti = TableQuery[Klijenti]
  val q1 = sql"select IME_KLIJENTA from KLIJENTI WHERE ID==1 ".as[String]

  val q2 = mojiKlijenti.filter(_.ID_KLIJENTA === 0)

  println(q1)
  println(q2)

在运行此代码之前,我已经通过phpmyadmin在数据库中插入了一行,因此值q1和q2都应该打印出实际值,但不是这样,我得到了这个:

background log: info: Running HelloSlick 
background log: info: slick.jdbc.SQLActionBuilder$$anon$1@56300388
background log: info: Rep(Filter @1636634026)

我的apache webserver和mysql已打开,以防万一有人问... 为什么我没有得到真正的价值观?

编辑产品:&gt;稍后,我尝试执行以下命令mojiKlijenti += Klijent(Some(5),"Name","Surname","Address")并再次编译成功但是当我在phpmyadmin中检查我的数据库时,没有添加任何记录..

1 个答案:

答案 0 :(得分:1)

您没有获得真正的值,因为您需要通过调用其结果方法将Query转换为Action并执行它。

val action = q2.result
val results = db.run( action)
results.foreach( println )