创建数据库是可行的。SQLite编辑器返回正确的值。但是,在代码下面运行不会返回结果。我添加了println("completed")以验证达到了这部分代码:
val setup = DBIO.seq((movies.schema ++ rooms.schema ++ screenings.schema).create,
movies += (1, "Django", "Quentin Tarantino"),
rooms += (1, 9, 10),
screenings += (1, 1, 1, 1, "19:15")
)
val setupFuture = Await.result(db.run(setup),20.second)
println("Screenings")
db.run(screenings.result).onComplete {
case Success(results) => println("completed") //results.foreach(println)
case Failure(exception) => println(s"An error has occured: ${exception.getMessage}")
}我得到:
[2022-11-11 09:51:05,463] [INFO] [com.zaxxer.hikari.HikariDataSource] [main] [] - db - Starting...
[2022-11-11 09:51:05,485] [INFO] [com.zaxxer.hikari.HikariDataSource] [main] [] - db - Start completed.
Screenings
Process finished with exit code 0我不知道为什么。
发布于 2022-11-11 13:57:50
在Slick中,所有的操作都是异步的。因此,在异步db读取线程完成之前,您的程序(=主线程)正在完成。因此,就像您等待setup完成一样,您可能会阻塞并等待查询,比如
...
println("Screenings")
val result = Await.result(db.run(screenings.result), 2.seconds)
println(s"Successfully read: ${result.size} elements")但是,由于阻塞在某种程度上违背了反应性编程的目的,所以您可以在程序的末尾锁定并以异步方式运行所有操作,比如:
db.run(setup).andThen { case _ =>
db.run(screenings.result).onComplete {
case Success(results) => println("completed") //results.foreach(println)
case Failure(exception) => println(s"An error has occured: ${exception.getMessage}")
}
}
// We don't want the main thread to finish
System.in.readhttps://stackoverflow.com/questions/74400186
复制相似问题