带有Slick和Play的SQLite

时间:2016-01-20 17:28:48

标签: scala sqlite playframework playframework-2.0 slick

所以我正在尝试修改其中一个类型安全的激活器模板,以使用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]

2 个答案:

答案 0 :(得分:0)

我有一个有趣,精巧的SQLite项目:https://github.com/aukgit/scala-open-real-time-bidding-rtb

它也有一个存储库模式。

请签出https://github.com/aukgit/scala-open-real-time-bidding-rtb/releases/tag/v0.0.5

给出一个示例,说明如何使用Slick与SQLite:

import slick.jdbc.SQLiteProfile.api._ // must import
lazy val db = Database.forURL(url = AbsoluteDatabasePath)

光滑的存储库模式 https://github.com/aukgit/scala-open-real-time-bidding-rtb/tree/6bf6beb6adb93b83cba49085d2d33269502189e1/app/shared/com/repository

您可以下载该版本中的存储库并使用SBT运行它

 run sbt

或使用IntelliJ IDEA打开

播放(https://github.com/aukgit/scala-open-real-time-bidding-rtb/blob/6bf6beb6adb93b83cba49085d2d33269502189e1/app/controllers/controllerRoutes/routerGeneric/RtbServiceBasicRouter.scala)的路由器示例

class RtbServiceBasicRouter @Inject()(
  controller : RequestSimulatorServiceApiController)
  extends SimpleRouter {

  val routingActionWrapper : ControllerGenericActionWrapper = ControllerGenericActionWrapper(
    ControllerDefaultActionType.Routing)

  override def routes : Routes = {
    try {
      case GET(p"/serviceName") | GET(p"/") =>
        controller.getServiceName()

      case GET(p"/commands") | GET(p"/available-commands") | GET(p"/routes") =>
        controller.getAvailableCommands()
      case GET(p"/bannerRequest") =>
        controller.getBannerRequestSample()
    } catch {
      case e : Exception =>
        controller.handleError(e, routingActionWrapper)
        throw e
    }
  }
}

将路由控制器添加到路由(https://github.com/aukgit/scala-open-real-time-bidding-rtb/blob/6bf6beb6adb93b83cba49085d2d33269502189e1/conf/routes

->         /services/v1/rtbSimulateService                  controllers.controllerRoutes.routerGeneric.RtbServiceBasicRouter

SBT软件包: https://github.com/aukgit/scala-open-real-time-bidding-rtb/blob/6bf6beb6adb93b83cba49085d2d33269502189e1/build.sbt

SBT中Sqlite的推荐软件包:

  "org.joda" % "joda-convert" % "2.2.1", // for time convert
  "com.github.tototoshi" %% "slick-joda-mapper" % "2.4.2", // 2.4 doesn't work
  "joda-time" % "joda-time" % "2.7",
  "org.xerial" % "sqlite-jdbc" % "3.30.1", // sqlite driver

Slick软件包,如果要集成:

  "com.typesafe.slick" %% "slick" % "3.3.2",
  "com.typesafe.slick" %% "slick-codegen" % "3.3.2", // for generating Table schema for sqlite db

sqlite数据库到表模式生成示例(数据库优先方法)[https://github.com/aukgit/scala-open-real-time-bidding-rtb/blob/6bf6beb6adb93b83cba49085d2d33269502189e1/app/shared/com/ortb/executors/DatabaseEngineCodeGenerator.scala]

slick.codegen.SourceCodeGenerator.run(
    profile = databaseGenerateConfig.profile,// "slick.jdbc.SQLiteProfile",
    jdbcDriver = databaseGenerateConfig.jdbcDriver, //"org.sqlite.JDBC",
    url = databaseGenerateConfig.compiledDatabaseUrl, //
    // "jdbc:sqlite:D:\\PersonalWork\\Github\\scala-rtb-example\\src\\main\\resources\\openRTBSample.db",
    outputDir = databaseGenerateConfig.compiledOutputDir, //
    // "D:\\PersonalWork\\Github\\scala-rtb-example\\src\\main\\scala\\com\\ortb\\persistent\\schema",
    pkg = databaseGenerateConfig.pkg,
    user = None,
    password = None,
    ignoreInvalidDefaults = true,
    outputToMultipleFiles = false
    )

答案 1 :(得分:0)

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

所有作品均基于以下示例: 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. application.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
}