Scala Slick在数据库中创建表

帕特里克·S。

我正在构建一个ScalaFX应用程序,并想将Slick添加为我的数据库框架。我对此很陌生,正在尝试仅使用Slick配置我的应用程序。我正在逐步关注在线教程。但是,当我执行db.run()时,永远不会创建表。我试过运行普通的sql查询,并且可以正常工作,这不是说它没有连接。它运行得很好,并返回“ Coffees:”。这是我的代码。提前致谢!

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._
import scala.slick.jdbc.{StaticQuery => Q}
import scala.concurrent.{Future, Await}
// The main application
object CustomerQueries extends App {
  val db = Database.forURL("jdbc:mysql://localhost:3306/business", driver="com.mysql.jdbc.Driver",
    user="pslagle12", password="pfSlagle12")
    val coffees = TableQuery[Coffees]
  val suppliers = TableQuery[Suppliers]
  //Q.updateNA("CREATE TABLE `company_name`").execute
      val customers = TableQuery[Customers]
  db.withSession {implicit session =>
    val setup = DBIO.seq(
      (suppliers.schema ++ coffees.schema).create,
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 (?,?,?,?,?,?)
      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 setupFuture = db.run(setup)
    // Read all coffees and print them to the console
    println("Coffees:")
    db.run(coffees.result).map(_.foreach {
      case (name, supID, price, sales, total) =>
        println("  " + name + "\t" + supID + "\t" + price + "\t" + sales + "\t" + total)
    })
    // Equivalent SQL code:
    // select COF_NAME, SUP_ID, PRICE, SALES, TOTAL from COFFEES

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


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

}
devkat

为确保结果查询在设置查询完成后运行,您应同时运行两个操作:

val resultFuture = db.run(setup >> coffees.result)

然后,如jkinkead建议的那样,如果要打印结果,请等待Future完成:

Await.result(resultFuture, 10 seconds)

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

在PLAY(Scala)中以JSON形式从数据库获取数据

来自分类Dev

Scala Slick表继承

来自分类Dev

如何使用Scala Slick连续访问数据库条目

来自分类Dev

更新数据库行scala slick

来自分类Dev

无法在数据库中创建表

来自分类Dev

Scala Slick数据库视图

来自分类Dev

工匠,在数据库中创建表

来自分类Dev

Scala的Slick Orm如何检索数据库列值

来自分类Dev

@JoinTable是否在数据库中创建持久表?

来自分类Dev

Slick可以从模型中自动在数据库中创建表(生成SQL并执行)吗?

来自分类Dev

将Scala Slick与数据库枚举一起使用

来自分类Dev

我数据库中的Scala Slick和复杂类型

来自分类Dev

无法在数据库中创建多个表

来自分类Dev

在PLAY(Scala)中以JSON形式从数据库获取数据

来自分类Dev

允许用户在数据库中创建表

来自分类Dev

如何使用Scala Slick连续访问数据库条目

来自分类Dev

在数据库中创建表

来自分类Dev

如何使用jQuery在数据库中创建表

来自分类Dev

无法在数据库中创建表

来自分类Dev

使用Slick和Scala将表添加到SQLite数据库

来自分类Dev

读取数据库表并输出到Scala中的文件?

来自分类Dev

Scala Slick在数据库中创建表

来自分类Dev

Scala Slick 将多个随机行插入到 PostgreSql 数据库中

来自分类Dev

Scala,播放 - 无法插入到 Postgresql 数据库中

来自分类Dev

Play+Scala 测试 Slick 数据库

来自分类Dev

无法在数据库中创建表

来自分类Dev

如何在 Slick 中的一系列数据库查询之间包含 scala 操作?

来自分类Dev

无法在数据库中创建表

来自分类Dev

在 Scala 中连接到多个数据库