无法从数据库中获取数据

时间:2017-10-04 21:49:43

标签: scala slick

当我第一次运行程序时,数据会被插入到表中,并且按预期工作。

现在请评论对setUp(db)方法的调用并运行它。这不会打印db中的行。为什么未来没有完成并打印表格中的行?

class Coffees(tag: Tag) extends Table[(String, Double)](tag, "COFFEES")                         
{
  def name = column[String]("COF_NAME", O.PrimaryKey)
  def price = column[Double]("PRICE")
  def * = (name, price)
}

object Main extends App {
  val db = Database.forConfig("mydb")
  *setUp(db)*
  query(db)
  def setUp(db: Database) = {
    val coffees = TableQuery[Coffees]
    val setup = DBIO.seq(coffees.schema.create, coffees += ("rohit", 100))
    val setUpFuture = db.run(setup)
    Await.result(setUpFuture, Duration.Inf)
  }

  def query(db: Database) = {
    val coffees = TableQuery[Coffees]
    val q = for (c <- coffees) yield c.*
    val a = q.result
    val f: Future[Seq[(String, Double)]] = db.run(a)
    f.onComplete {
      lst => {
        for(e <-lst) println(s"${e}")
      }
    }
    Await.ready(f, 100 second)
  }

  def queryWithStreams(db: Database) = {
    val coffees = TableQuery[Coffees]
    val query = for(c <- coffees) yield c.name
    val p: DatabasePublisher[String] = db.stream(query.result)
    p.foreach { s => println(s"Element: $s") }
  }
}

0 个答案:

没有答案