SQLite 与 Slick and Play

Posted

技术标签:

【中文标题】SQLite 与 Slick and Play【英文标题】:SQLite with Slick and Play 【发布时间】:2016-04-26 16:30:46 【问题描述】:

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

【问题讨论】:

slick.driver.SQLiteDriver 更改为 slick.driver.SQLiteDriver$。如果这没有帮助,请添加堆栈跟踪的根本原因。 感谢您的评论!从某种意义上说,它确实有所帮助,因为我现在遇到了一个不同的错误[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] 也许在您的applicaion.conf 中设置一个测试查询有助于:slick.dbs.default.db.connectionTestQuery="select 1" 离题:在 SO 上,通常使用新发现来编辑问题,而不是在 cmets 中添加它们。这样其他用户可以更轻松地帮助您解决问题:) 好吧,我想我现在放弃了。您的建议有所帮助,但我遇到了其他问题,现在它并不是项目的重要组成部分。感谢您的帮助 【参考方案1】:

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

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

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

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

【讨论】:

【参考方案2】:

我有 play、slick、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)

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

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

 run sbt

或使用 IntelliJ IDEA 打开

Play 的路由器示例 (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 Packages:

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

sqlite db 到表模式生成示例(数据库优先方法)[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
    )

【讨论】:

以上是关于SQLite 与 Slick and Play的主要内容,如果未能解决你的问题,请参考以下文章

将 Slick 与 Kudu/Impala 一起使用

React-slick 与 gatsby-plugin-image

将 React-Slick 与查询参数同步

Scala Slick 无法将查询与计数联合起来

Fluent NHibernate and Mysql,SQLite

Slick:如何将 SQL LIKE 语句与 SQL IN 语句结合使用