gpt4 book ai didi

scala - 带有 Slick and Play 的 SQLite

转载 作者:行者123 更新时间:2023-12-04 18:01:21 26 4
gpt4 key购买 nike

所以我正在尝试修改其中一个类型安全激活器模板以使用 SQLite 数据库而不是内置的 H2 数据库。这是原始模板 https://github.com/playframework/playframework/tree/master/templates/play-scala-intro

我所做的是更改 application.conf 文件以包含这些行:

slick.dbs.default.driver=slick.driver.SQLiteDriver
slick.dbs.default.db.driver=org.sqlite.JDBC
slick.dbs.default.db.url="jdbc:sqlite:/home/marcin/play-scala-intro/people.db"

当然,我还创建了文件本身(只是touch people.db)。然后,如果我启动我的应用程序,我会收到以下错误:

[info] ! @6ooe822f0 - Internal server error, for (GET) [/] ->
[info]
[info] play.api.Configuration$$anon$1: Configuration error[Cannot connect to database [default]]
[info] at play.api.Configuration$.configError(Configuration.scala:178) ~[play_2.11-2.4.6.jar:2.4.6]
[info] at play.api.Configuration.reportError(Configuration.scala:829) ~[play_2.11-2.4.6.jar:2.4.6]
[info] at play.api.db.slick.DefaultSlickApi$DatabaseConfigFactory.create(SlickApi.scala:93) ~[play-slick_2.11-1.1.1.jar:1.1.1]
[info] at play.api.db.slick.DefaultSlickApi$DatabaseConfigFactory.get$lzycompute(SlickApi.scala:81) ~[play-slick_2.11-1.1.1.jar:1.1.1]
[info] at play.api.db.slick.DefaultSlickApi$DatabaseConfigFactory.get(SlickApi.scala:80) ~[play-slick_2.11-1.1.1.jar:1.1.1]

我正在寻找一些示例如何像这里一样设置它 https://groups.google.com/forum/#!msg/scalaquery/07JBbnZ5VZk/7D1_5N4uGjsJ

或在这里:

https://github.com/playframework/play-slick

但它们与我的代码不够相似,而且由于我是新手,所以我真的不知道如何使用它们。帮助赞赏,谢谢!

[编辑]:

根据评论的建议,我在驱动程序名称的末尾添加了“$”,conf 文件中的内容现在如下所示:

slick.dbs.default.driver=slick.driver.SQLiteDriver$
slick.dbs.default.db.driver=org.sqlite.JDBC
slick.dbs.default.db.url="jdbc:sqlite:/home/marcin/play-scala-intro/people.db"

在出现另一个错误的意义上,这是可行的:

[info] Caused by: java.sql.SQLException: JDBC4 Connection.isValid() method not supported, connection test query must be configured 
[info] at com.zaxxer.hikari.pool.BaseHikariPool.addConnection(BaseHikariPool.java:441) ~[HikariCP-java6-2.3.7.jar:na]
[info] at com.zaxxer.hikari.pool.BaseHikariPool$1.run(BaseHikariPool.java:413) ~[HikariCP-java6-2.3.7.jar:na]
[info] at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:511) ~[na:1.8.0_66]

最佳答案

是的,这是一个很老的问题,但也许答案对某些人有用。

所有作品均基于此处提供的示例: https://developer.lightbend.com/start/?group=play&project=play-samples-play-scala-slick-example

我已经使用 Scala/Play/Slick 执行以下步骤成功运行了 SQLite 数据库:

  1. build.sbt 文件:
lazy val root = (project in file("."))
.enablePlugins(PlayScala)
.settings(
name := """Application""",
version := "2.8.x",
scalaVersion := "2.13.1",
libraryDependencies ++= Seq(
guice,
"org.playframework.anorm" %% "anorm" % "2.6.5",
"com.typesafe.play" %% "play-slick" % "5.0.0",
"com.typesafe.play" %% "play-slick-evolutions" % "5.0.0",
"org.xerial" % "sqlite-jdbc" % "3.31.1",
specs2 % Test,
),
scalacOptions ++= Seq(
"-feature",
"-deprecation",
"-Xfatal-warnings"
)
)
  1. 应用程序.conf
slick.dbs.default.profile="slick.jdbc.SQLiteProfile$"
slick.dbs.default.db.profile="slick.driver.SQLiteDriver"
slick.dbs.default.db.url="jdbc:sqlite:/mnt/comments.db"
slick.dbs.default.db.driver=org.sqlite.JDBC

请注意,它也适用于相对路径的情况:

slick.dbs.default.profile="slick.jdbc.SQLiteProfile$"
slick.dbs.default.db.profile="slick.driver.SQLiteDriver"
slick.dbs.default.db.url="jdbc:sqlite:./comments.db"
slick.dbs.default.db.driver=org.sqlite.JDBC

Play Evolution 也有效:

play.evolutions {
db.default.enabled = true
}

关于scala - 带有 Slick and Play 的 SQLite,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34906635/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com