2016-05-06 4 views
2

私はScalaFXアプリケーションを構築しており、データベースフレームワークとしてSlickを追加したいと考えています。私はそれに非常に新しいとSlickで私のアプリを設定しようとしています。私はステップバイステップでオンラインチュートリアルに従っています。しかし、db.run()を実行すると、テーブルが作成されることはありません。私はプレーンSQLクエリを実行しようとしているので、それは接続していないことではないです。それはうまく動作し、ちょうど "コーヒーを返します:"私のコードはここにあります。前もって感謝します!その後、Scala Slickデータベース内のテーブルを作成

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

そして、jkinkead未来が完了するのを待って、提案のように、:セットアップクエリが完了した後、あなたは両方のアクションを一緒に実行する必要があり、結果のクエリが実行されることを保証するために

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

} 
+1

'db.run'は' Future'を返します。 'scala.concurrent.Await'を使って実行が終了するまでブロックしてみてください。 – jkinkead

+0

助けてくれてありがとう! –

答えて

1

結果が印刷されるようにするには:

Await.result(resultFuture, 10 seconds) 
関連する問題