wulftone
wulftone

Reputation: 1708

Spray.io, slick, sqlite3: no suitable driver found

I'm a super noob at the java/scala ecosystem, and I'm having trouble getting this example project to work.

package com.example

import akka.actor.Actor
import spray.routing._
import spray.http._
import MediaTypes._
import spray.json._
import DefaultJsonProtocol._
import scala.slick.driver.SQLiteDriver.simple._

class MyServiceActor extends Actor with MyService {

  def actorRefFactory = context

  def receive = runRoute(myRoute)
}


trait MyService extends HttpService {
  val cars: TableQuery[Cars] = TableQuery[Cars]

  val db = Database.forURL("jdbc:sqlite:./test.sqlite3", driver = "scala.slick.driver.SQLiteDriver")

  val myRoute = path("") {
    get {
      respondWithMediaType(`application/json`) {
        complete {

          val result = db.withSession {

            implicit session =>

              cars.ddl.create
              val myCar = Car(-1, "Ford", "Taurus", 2015)
              cars.insert(myCar)
          }
          "Hi"
        }
      }
    }
  }
}

Here's the project. It's based directly off of the example from spray.io.

I'm getting a spray java.sql.SQLException: No suitable driver found for jdbc:sqlite:./test.sqlite3 error. Obviously I haven't loaded the driver correctly, but I have scoured the internet for the correct syntax and failed miserably to find anything. What am I missing?

Thanks!

p.s. Scala 2.11.2, Spray 1.3.2, Slick 2.1.0.

Upvotes: 2

Views: 539

Answers (1)

j-keck
j-keck

Reputation: 1031

  1. You need to add the sqlite jdbc driver to your dependencies:

update your build.sbt to:

libraryDependencies ++= {
  val akkaV = "2.3.6"
  val sprayV = "1.3.2"
  Seq(
    "com.typesafe.akka"   %%  "akka-actor"    % akkaV,
    "com.typesafe.akka"   %%  "akka-testkit"  % akkaV    % "test",
    "com.typesafe.slick"  %%  "slick"         % "2.1.0",
    "io.spray"            %%  "spray-can"     % sprayV,
    "io.spray"            %%  "spray-json"    % "1.3.1",
    "io.spray"            %%  "spray-routing" % sprayV,
    "io.spray"            %%  "spray-testkit" % sprayV   % "test",
    "org.slf4j"           %   "slf4j-nop"     % "1.6.4",
    "org.specs2"          %%  "specs2-core"   % "2.3.11" % "test",
    "org.xerial"          %   "sqlite-jdbc"   % "3.7.2"            // << SQLite JDBC Driver
  )
}

2. Load the driver in your 'MyService' trait:

 Class.forName("org.sqlite.JDBC") // load the sqlite jdbc driver (google for Class.forName)
 val db = Database.forURL(.....

Upvotes: 2

Related Questions