【问题标题】:Slick doesnt input data in h2 databaseSlick不在h2数据库中输入数据
【发布时间】:2016-02-12 12:55:28
【问题描述】:

我现在被这个问题困扰了几天。我有正确的连接和正确的查询,但是当我运行应用程序时,我的数据库中没有任何变化,可能是它在 localhost:9000 上运行我的 html 网页而不是执行 Slick 查询吗? 我正在使用 IntelliJ IDEA 1.15 和 Typesafe Activator

构建:

libraryDependencies ++= Seq(
jdbc,
cache,
ws,
specs2 % Test,
"com.typesafe.slick" %% "slick" % "3.1.0-RC2",
"org.slf4j" % "slf4j-nop" % "1.7.10",
"org.slf4j" % "jcl-over-slf4j"  % "1.7.10",
"com.h2database" % "h2" % "1.4.187",
"org.scalatest" %% "scalatest" % "2.2.4" % "test"
).map(_.force())
// login embed:       jdbc:h2:~/test1
// login servermode:  jdbc:h2:tcp://localhost/~/test1
libraryDependencies += evolutions

Application.conf:

h2mem1 = {
url = "jdbc:h2:mem:test1"
driver = org.h2.Driver
connectionPool = disabled
keepAliveConnection = true
}

db.default.driver=org.h2.Driver
db.default.url="jdbc:h2:mem:test1;MODE=MYSQL;DB_CLOSE_DELAY=-1"
db.default.username=sa
db.default.password=""

Baris 数据库:

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

// The main application
object BarisDatabase extends App {
val db = Database.forURL("jdbc:h2:mem:test1;MODE=MYSQL;DB_CLOSE_DELAY=-1", driver="org.h2.Driver")
try {

// The query interface for the Suppliers table
val suppliers: TableQuery[Suppliers] = TableQuery[Suppliers]

// the query interface for the Coffees table
val coffees: TableQuery[Coffees] = TableQuery[Coffees]

val setupAction: DBIO[Unit] = DBIO.seq(
  // Create the schema by combining the DDLs for the Suppliers and Coffees
  // tables using the query interfaces
  (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")
)

val setupFuture: Future[Unit] = db.run(setupAction)
val f = setupFuture.flatMap { _ =>

  // Insert some coffees (using JDBC's batch insert feature)
  val insertAction: DBIO[Option[Int]] = 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)
  )

  val insertAndPrintAction: DBIO[Unit] = insertAction.map { coffeesInsertResult =>
    // Print the number of rows inserted
    coffeesInsertResult foreach { numRows =>
      println(s"Inserted $numRows rows into the Coffees table")
    }
  }

  val allSuppliersAction: DBIO[Seq[(Int, String, String, String, String, String)]] =
    suppliers.result

  val combinedAction: DBIO[Seq[(Int, String, String, String, String, String)]] =
    insertAndPrintAction >> allSuppliersAction

  val combinedFuture: Future[Seq[(Int, String, String, String, String, String)]] =
    db.run(combinedAction)

  combinedFuture.map { allSuppliers =>
    allSuppliers.foreach(println)
  }
  }
  } finally db.close
  }

Tables.scala:

import slick.driver.H2Driver.api._
import slick.lifted.{ProvenShape, ForeignKeyQuery}

// A Suppliers table with 6 columns: id, name, street, city, state, zip class Suppliers(tag: Tag)
extends Table[(Int, String, String, String, String, String)](tag, "SUPPLIERS") {

// This is the primary key column:
def id: Rep[Int] = column[Int]("SUP_ID", O.PrimaryKey)
def name: Rep[String] = column[String]("SUP_NAME")
def street: Rep[String] = column[String]("STREET")
def city: Rep[String] = column[String]("CITY")
def state: Rep[String] = column[String]("STATE")
def zip: Rep[String] = column[String]("ZIP")

// Every table needs a * projection with the same type as the table's type parameter
def * : ProvenShape[(Int, String, String, String, String, String)] =
(id, name, street, city, state, zip)
}

// A Coffees table with 5 columns: name, supplier id, price, sales, total
class Coffees(tag: Tag)
extends Table[(String, Int, Double, Int, Int)](tag, "COFFEES") {

def name: Rep[String] = column[String]("COF_NAME", O.PrimaryKey)
def supID: Rep[Int] = column[Int]("SUP_ID")
def price: Rep[Double] = column[Double]("PRICE")
def sales: Rep[Int] = column[Int]("SALES")
def total: Rep[Int] = column[Int]("TOTAL")

def * : ProvenShape[(String, Int, Double, Int, Int)] = (name, supID, price, sales, total)

// A reified foreign key relation that can be navigated to create a join
def supplier: ForeignKeyQuery[Suppliers, (Int, String, String, String, String, String)] =
foreignKey("SUP_FK", supID, TableQuery[Suppliers])(_.id)
}

我从 Typesafe Activator 中的教程 Hello-Slick-3.1 中获得了大部分代码。 Filestructure in IntelliJ IDEA

我希望这是足够的信息

【问题讨论】:

    标签: database scala playframework-2.0 h2 slick-3.0


    【解决方案1】:

    尝试改变:

    val db = Database.forURL("jdbc:h2:mem:test1;MODE=MYSQL;DB_CLOSE_DELAY=-1", driver="org.h2.Driver")
    

    val db = Database.forConfig("h2mem1")
    

    【讨论】:

    • H2 是一个内存数据库。这意味着您写入的所有数据都会进入模拟真实数据库的内存中。您是否尝试使用 MySQL 数据库或在将数据写入内存数据库时失败?
    • 我在我的 h2 数据库中写入数据失败,另外我更改了我的数据库的 URL,现在它将表放在我的主目录 jdbc:h2:tcp://localhost/c:/users /Creativename/test1
    • 再次写入内存,而不是硬盘驱动器或真实数据库。您的程序所做的所有事情都会创建几个实体并将它们打印到控制台。如果您在控制台中没有看到它们,请从 app.conf 中删除所有 db.default 行,并将 Database.forConfig("h2mem1") 保留为您的 db 配置。
    猜你喜欢
    • 2018-08-05
    • 2020-02-18
    • 2014-11-25
    • 1970-01-01
    • 2013-08-20
    • 1970-01-01
    • 1970-01-01
    • 2022-01-20
    • 1970-01-01
    相关资源
    最近更新 更多