scala - Spray.io、光滑、sqlite3 : no suitable driver found

标签 scala sqlite slick

我是 java/scala 生态系统的 super 菜鸟,但我在让这个示例项目正常工作时遇到了困难。

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 。它直接基于 Spray.io 的示例。

我收到spray java.sql.SQLException: No合适的驱动程序找到jdbc:sqlite:./test.sqlite3错误。显然我没有正确加载驱动程序,但我在互联网上搜索了正确的语法,但悲惨地找不到任何东西。我错过了什么?

谢谢!

附: Scala 2.11.2、Spray 1.3.2、Slick 2.1.0。

最佳答案

  1. 您需要将 sqlite jdbc 驱动程序添加到您的依赖项中:

将您的 build.sbt 更新为:

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。在“MyService”特征中加载驱动程序:

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

关于scala - Spray.io、光滑、sqlite3 : no suitable driver found,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27044946/

相关文章:

objective-c - 尝试在表中插入值时数据库被锁定?

android - 连接两个表并替换值

scala - 尝试按 UUID 过滤时出错 - Slick

c++ - 如何从另一个平台(iOS 到 Windows)打开和读取 SQLite 数据库

scala - Slick 3 Transactions with logic in Scala

mysql - Scala Slick 分页编译,但在运行时崩溃

java - 执行 .jar 文件时, `scala` 是否不需要 -jar,而 `java` 则需要?

scala - 为什么我不能从内部的任一投影中提取元组以使用模式匹配进行理解?

IntelliJ IDEA 中的 Scala 控制台要么接受输入,要么打印输出,命令从 temrinal 运行

scala - 使用 future 时程序不会终止