问题描述
我正在尝试在我的Build.scala
中使用 sqlite-jdbc驱动程序一个带有一些必要表的sqlite db,然后再进行编译.这是我为实现这一目标而写的:
I'm trying to use the sqlite-jdbc driver in my Build.scala
to generate a sqlite db with some necessary tables before compilation. This is what I wrote to achieve that:
compile in Compile <<= (compile in Compile) map { result =>
val sqliteDb = file("my_sqlite.db")
if (!sqliteDb.exists()) {
val connection = DriverManager.getConnection(s"jdbc:sqlite:${sqliteDb.getAbsolutePath}")
val statement = connection.prepareStatement("create table EXAMPLE ( ... );")
statement.execute()
statement.close()
connection.close()
}
result
}
这很好,但是当我运行compile
时,出现此错误:
That's all well and good, but when I run compile
I get this error:
[error] (my-project/compile:compile) java.sql.SQLException: No suitable driver found for jdbc:sqlite:/Users/2rs2ts/src/my-project/my_sqlite.db
现在,这有点令人沮丧,因为我认为我可以通过创建递归项目将这种依赖关系添加到Build.scala
的类路径中.我的目录结构如下:
Now that's a bit frustrating since I thought I could add that dependency to Build.scala
's classpath by creating a recursive project. My directory structure looks like this:
my-project/
project/
Build.scala
build.sbt
project/
build.sbt
my-project/project/project/build.sbt
看起来像这样:
libraryDependencies += "org.xerial" % "sqlite-jdbc" % "3.8.10.1"
编辑:我也将该行放在my-project/project/build.sbt
中,但不能解决我的问题.
Edit: I also put that line in my-project/project/build.sbt
and it did not resolve my issue.
那么...我做错了什么?为了使sqlite驱动程序正常工作,我需要对类路径具有这种依赖性.
So... what did I do wrong? I need this dependency on the classpath in order to get the sqlite driver to work.
推荐答案
您走了两步太远了.将libraryDependencies
从my-project/project/project/build.sbt
移到my-project/project/build.sbt
,以便在my-project/project/Build.scala
中可用.
You went two steps too far. Move that libraryDependencies
from my-project/project/project/build.sbt
to my-project/project/build.sbt
that way it will be available in my-project/project/Build.scala
.
这篇关于如何将库依赖项添加到Build.scala的类路径中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!