Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SQLite with Slick and Play

So I'm trying to modify one of the typesafe activator templates to use an SQLite database instead of the built in H2 one. Here is the original template https://github.com/playframework/playframework/tree/master/templates/play-scala-intro

What I've done is to change the application.conf file to have these lines:

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"

Of course I also created the file itself (just did touch people.db). Then if I start my application I am getting the following error:

[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]

I was looking for some examples how to set it up like here https://groups.google.com/forum/#!msg/scalaquery/07JBbnZ5VZk/7D1_5N4uGjsJ

or here:

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

but they weren't similar enough to my code and since I'm new to all this I couldn't really figure out how to use them. Help appreciated, thanks!

[EDIT]:

following a suggestion from the comment I added "$" at the end of the driver name, to that what's in the conf file now looks like this:

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"

That works in the sense that another error comes up:

[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]
like image 835
marcin_j Avatar asked Mar 30 '26 15:03

marcin_j


1 Answers

Yes, this is quite old question, but maybe the answer can be useful for someone.

All the works is based upon the example presented here: https://developer.lightbend.com/start/?group=play&project=play-samples-play-scala-slick-example

I've successfully run SQLite database with Scala/Play/Slick performing the following steps:

  1. build.sbt file:
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

Please note, that it also works in the case for the relative path:

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 also works:

play.evolutions {
  db.default.enabled = true
}
like image 193
francisco Avatar answered Apr 02 '26 12:04

francisco



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!