SQLException:具有SQLite和Scala的未知数据库
我有对象字典sqllitedao,它应该创建一个在初始化期间不存在的表。但我有一个例外,尽管数据库是在文件系统中创建的。帮我找出原因。这是我的密码:SQLException:具有SQLite和Scala的未知数据库,scala,sqlite,Scala,Sqlite,我有对象字典sqllitedao,它应该创建一个在初始化期间不存在的表。但我有一个例外,尽管数据库是在文件系统中创建的。帮我找出原因。这是我的密码: object DictionarySqlLiteDao extends Dao[Word] { private val CREATE_WORD_TABLE_SQL = "CREATE TABLE IF NOT EXISTS thelper.WORD " + "(ID INTEGER PRIMARY KEY AUTOINCREMEN
object DictionarySqlLiteDao extends Dao[Word] {
private val CREATE_WORD_TABLE_SQL = "CREATE TABLE IF NOT EXISTS thelper.WORD " +
"(ID INTEGER PRIMARY KEY AUTOINCREMENT, " +
"WORD TEXT NOT NULL, " +
"UKR TEXT NOT NULL, " +
"ENG TEXT NOT NULL, " +
"EXAMPLE TEXT)"
private val SAVE_WORD_SQL = "INSERT INTO WORD(WORD, UKR, ENG, EXAMPLE" +
"VALUES(?, ?, ?, ?)"
private val connection: Connection = {
Class.forName("org.sqlite.JDBC")
DriverManager.getConnection("jdbc:sqlite:thelper")
}
{
connection.createStatement().executeUpdate(CREATE_WORD_TABLE_SQL)
}
override def save(word: Word): Word = {
val statement = connection.prepareStatement(SAVE_WORD_SQL, Statement.RETURN_GENERATED_KEYS)
statement.setString(1, word.word)
statement.setString(2, word.translation("ukr"))
statement.setString(3, word.translation("eng"))
statement.setString(4, word.example.orNull)
statement.executeUpdate()
val id = statement.getGeneratedKeys.getInt(1)
word.copy(id = id)
}
def main(args: Array[String]) {
println(connection == null) // here I get false
println(connection.createStatement().execute("select date('now')")) // here I get true
val w = save(Word(-1, "germanWord", Map("ukr" -> "ukrTranslation", "eng" -> "engTranslation"), Option.empty[String])) // and here I get an Exception
println(w)
}
}
我得到一个例外:
Exception in thread "main" java.lang.ExceptionInInitializerError
at thelper.Dictionary.dao.DictionarySqlLiteDao.main(DictionarySqlLiteDao.scala)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:497)
at com.intellij.rt.execution.application.AppMain.main(AppMain.java:144)
Caused by: java.sql.SQLException: unknown database thelper
at org.sqlite.core.NativeDB.throwex(NativeDB.java:397)
at org.sqlite.core.NativeDB._exec(Native Method)
at org.sqlite.jdbc3.JDBC3Statement.executeUpdate(JDBC3Statement.java:116)
at thelper.Dictionary.dao.DictionarySqlLiteDao$.<init>(DictionarySqlLiteDao.scala:23)
at thelper.Dictionary.dao.DictionarySqlLiteDao$.<clinit>(DictionarySqlLiteDao.scala)
... 6 more
线程“main”java.lang.ExceptionInInitializeError中的异常
在thelper.Dictionary.dao.DictionarySqlLiteDao.main(DictionarySqlLiteDao.scala)
在sun.reflect.NativeMethodAccessorImpl.invoke0(本机方法)处
位于sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
在sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)中
位于java.lang.reflect.Method.invoke(Method.java:497)
位于com.intellij.rt.execution.application.AppMain.main(AppMain.java:144)
原因:java.sql.SQLException:未知数据库服务器
位于org.sqlite.core.NativeDB.throwex(NativeDB.java:397)
位于org.sqlite.core.NativeDB.\u exec(本机方法)
位于org.sqlite.jdbc3.JDBC3Statement.executeUpdate(JDBC3Statement.java:116)
在thelper.Dictionary.dao.DictionarySqlLiteDao$(DictionarySqlLiteDao.scala:23)
在thelper.Dictionary.dao.DictionarySqlLiteDao$(DictionarySqlLiteDao.scala)
... 还有6个
据我所知,您应该指定数据库的完整路径,在内存中,它应该是
thelper.db
类似于:
DriverManager.getConnection("jdbc:sqlite:thelper.db")`
注意:在此之后,您可以省略数据库名称:“如果不存在,则创建表”+…
p.p.S您可以使用三重引号来编写多行字符串,如
"""CREATE TABLE IF NOT EXISTS WORD(
| ID INTEGER PRIMARY KEY AUTOINCREMENT,
| WORD TEXT NOT NULL,
| UKR TEXT NOT NULL,
| ENG TEXT NOT NULL,
| EXAMPLE TEXT)""".stripMargin
可能与您的异常无关,但您的保存使用SQL
“插入WORD(WORD,UKR,ENG,EXAMPLEVALUES(?,,?,?)”
,示例和值之间没有括号。是的,真的。谢谢。但不幸的是,这与我的主要问题无关。