Scala Slick不读取或写入H2数据库

时间:2018-02-26 09:09:19

标签: scala h2 slick

我为OSX安装了H2,我可以通过打开jar文件来打开控制台。 我跟随Slick的getting started instructions并最终获得了以下最终代码:

package com.abc.xyz

import slick.driver.H2Driver.api._
import scala.concurrent.ExecutionContext.Implicits.global

object TestSlick extends App {
  val db = Database.forConfig("h2mem1")
  try {
    // Definition of the SUPPLIERS table
    class Suppliers(tag: Tag) extends Table[(Int, String, String, String, String, String)](tag, "SUPPLIERS") {
      def id = column[Int]("SUP_ID", O.PrimaryKey) // This is the primary key column
      def name = column[String]("SUP_NAME")
      def street = column[String]("STREET")
      def city = column[String]("CITY")
      def state = column[String]("STATE")
      def zip = column[String]("ZIP")
      // Every table needs a * projection with the same type as the table's type parameter
      def * = (id, name, street, city, state, zip)
    }
    val suppliers = TableQuery[Suppliers]

    // Definition of the COFFEES table
    class Coffees(tag: Tag) extends Table[(String, Int, Double, Int, Int)](tag, "COFFEES") {
      def name = column[String]("COF_NAME", O.PrimaryKey)
      def supID = column[Int]("SUP_ID")
      def price = column[Double]("PRICE")
      def sales = column[Int]("SALES")
      def total = column[Int]("TOTAL")
      def * = (name, supID, price, sales, total)
      // A reified foreign key relation that can be navigated to create a join
      def supplier = foreignKey("SUP_FK", supID, suppliers)(_.id)
    }
    val coffees = TableQuery[Coffees]

    val setup = DBIO.seq(
      // Create the tables, including primary and foreign keys
      (suppliers.schema ++ coffees.schema).create,

      // Insert some suppliers
      suppliers += (101, "Acme, Inc.",      "99 Market Street", "Groundsville", "CA", "95199"),
      suppliers += ( 49, "Superior Coffee", "1 Party Place",    "Mendocino",    "CA", "95460"),
      suppliers += (150, "The High Ground", "100 Coffee Lane",  "Meadows",      "CA", "93966"),
      // Equivalent SQL code:
      // insert into SUPPLIERS(SUP_ID, SUP_NAME, STREET, CITY, STATE, ZIP) values (?,?,?,?,?,?)

      // Insert some coffees (using JDBC's batch insert feature, if supported by the DB)
      coffees ++= Seq(
        ("Colombian",         101, 7.99, 0, 0),
        ("French_Roast",       49, 8.99, 0, 0),
        ("Espresso",          150, 9.99, 0, 0),
        ("Colombian_Decaf",   101, 8.99, 0, 0),
        ("French_Roast_Decaf", 49, 9.99, 0, 0)
      )
      // Equivalent SQL code:
      // insert into COFFEES(COF_NAME, SUP_ID, PRICE, SALES, TOTAL) values (?,?,?,?,?)
    )

    val setupFuture = db.run(setup)

    println("Coffees:")
    db.run(coffees.result).map(_.foreach {
      case (name, supID, price, sales, total) =>
        println("  " + name + "\t" + supID + "\t" + price + "\t" + sales + "\t" + total)
    })
  } finally db.close
}

在application.conf中,我有以下条目:

h2mem1 = {
  url = "jdbc:h2:~/test"
  driver = org.h2.Driver
  connectionPool = disabled
  keepAliveConnection = true
  DB_CLOSE_DELAY=-1
}

程序在没有警告或错误的情况下运行,但未列出插入的咖啡。它只显示字符串“Coffees:”并退出,退出代码为0.使用H2控制台检查我发现给定的数据库确实为空,因此插入数据不起作用。

由于没有警告或错误,我不知道从哪里开始。 H2数据库设置有问题吗?我该怎么知道?

2 个答案:

答案 0 :(得分:1)

上面代码的问题是origin会返回未来,只有在未来完成后才能查询您的表。如果在db.run(setup)返回的未来完成之前尝试查询,则不会在数据库中包含数据。所以将代码更改为

db.run(setup)

在上面的代码中。我只在db.run(setup)future完成后才执行查询。

此外,查询的返回值也是未来,因此我需要添加await调用以确保已返回所有结果,然后才尝试打印结果。

答案 1 :(得分:1)

感谢其他人的回答。实际上,人们应该与期货合作以确保它已经完成。但是,就我而言,事实证明这是另一个问题。

只有在从connectionPool=disabled文件中删除application.conf行后,编译器才会告诉我找不到驱动程序org.h2.Driver。我不得不添加

libraryDependencies += "com.h2database" % "h2" % "1.4.196"

到我的sbt文件。