我的数据库存储在Slick中的哪个位置? [Scala] [Slick 3.0]

时间:2015-07-08 20:41:05

标签: scala slick slick-3.0

我不太确定这是否是一个有效的问题,但我想知道我的数据库在Slick中存储的确切位置

例如,如果我按照http://slick.typesafe.com/doc/3.0.0/gettingstarted.html

上的示例进行操作

他们创建表格:

// 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)

数据库是否完全存在于内存中?

如果我从文本文件填写数据库,数据库是否仍然只存在于内存中? 如果是这样,我将如何将其传输到磁盘? 我甚至在这里走在正确的轨道上吗?

1 个答案:

答案 0 :(得分:1)

Slick不会在任何地方“存储”数据库。 Slick是一个允许您访问数据库中数据的库。您通常使用jdbc“连接URL”指定要连接的数据库,例如“jdbc:h2:mem:test1”。 Jdbc是用于访问关系数据库的标准Java API,并且存在许多存在jdbc驱动程序的数据库。

因此,当使用Slick时,您将选择使用哪个数据库:h2,postgres,mysql,oracle,sql server等。您应该查阅您选择的数据库文档以了解它存储您的数据。

我认为Slick选择使用h2作为入门示例的原因是h2可以在“内存模式”下运行,其中数据仅保存在内存中。在此模式下,数据纯粹是瞬态的,并在程序终止时丢失。另一方面,在数据存储在磁盘上的持久模式下运行h2也非常容易。此外,您可以选择运行数据库“embedded”或“client / server”。

你真的应该参考h2文档来了解更多(http://www.h2database.com/html/main.html)。特别是,jdbc连接URL使您可以控制是连接到瞬态,内存实例,嵌入式实例还是远程实例。请参阅http://www.h2database.com/html/features.html#database_url

相关问题