【发布时间】:2014-01-18 11:27:54
【问题描述】:
在我升级到 JAVA 7 之前,我将 sqlitedsb 附加到另一个的代码运行良好(我的猜测) 如何创建从 java 附加数据库的语法?我的语法使用 SQL 工具可以正常工作,但从 JAVA 中我没有成功。
我的代码:
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.sql.Statement;
public class TesteAttachDB {
public static void main(String[] args) {
// TODO Auto-generated method stub
testattach();
}
public static void testattach(){
Connection connection_historymapsdb = null;
String sTargetDB="C://temp//historydb11_maps_en.db";
try {
Class.forName("org.sqlite.JDBC");
connection_historymapsdb = DriverManager.getConnection("jdbc:sqlite:" + sTargetDB);
} catch (ClassNotFoundException e1) {
e1.printStackTrace();
} catch (SQLException e2) {
e2.printStackTrace();
}
Statement statement;
try {
//String sDatabasetoattach="C://temp//test.db";
//String sDatabasetoattach="C:/temp/test.db";
String sDatabasetoattach="C:\temp\test.db";
//String sDatabasetoattach="C:\temp\\userdb\test.db";
statement = connection_historymapsdb.createStatement();
String sSQL="Attach '" + sDatabasetoattach + "' as chronica_maps_tests";
System.out.println(sSQL);
statement.execute(sSQL);
String sTestSQL="select count(*) from chronica_maps_tests.testtable";
statement.execute(sTestSQL);
System.out.println("worked.");
} catch (SQLException e) {
e.printStackTrace();
}
}
}
错误信息:
答:
Attach 'C://temp//test.db' as chronica_maps_tests
java.sql.SQLException: unable to open database: C://temp//test.db
at org.sqlite.DB.execute(DB.java:275)
at org.sqlite.Stmt.exec(Stmt.java:56)
at org.sqlite.Stmt.execute(Stmt.java:83)
at maps.TesteAttachDB.testattach(TesteAttachDB.java:48)
at maps.TesteAttachDB.main(TesteAttachDB.java:15)
乙:
Attach 'C:/temp/test.db' as chronica_maps_tests
java.sql.SQLException: unable to open database: C:/temp/test.db
at org.sqlite.DB.execute(DB.java:275)
at org.sqlite.Stmt.exec(Stmt.java:56)
at org.sqlite.Stmt.execute(Stmt.java:83)
at maps.TesteAttachDB.testattach(TesteAttachDB.java:48)
at maps.TesteAttachDB.main(TesteAttachDB.java:15)
C:
Attach 'C: emp est.db' as chronica_maps_tests
java.sql.SQLException: no such table: chronica_maps_tests.testtable
hat funktioniert !
at org.sqlite.DB.throwex(DB.java:288)
at org.sqlite.NestedDB.prepare(NestedDB.java:115)
at org.sqlite.DB.prepare(DB.java:114)
at org.sqlite.Stmt.execute(Stmt.java:82)
at maps.TesteAttachDB.testattach(TesteAttachDB.java:52)
at maps.TesteAttachDB.main(TesteAttachDB.java:15)
我为 sSQL 尝试了不同的语法(A、B、C),但没有任何效果。 SQL-String 应该如何?
【问题讨论】:
-
你有权限吗?在不同的驱动器中尝试。
-
是的,我可以访问。已经试过了。那不是问题。
-
您正在使用单引号 ('),请尝试在文件名周围使用双引号 (")(也显示在 this 答案中)。甚至可能是 "C:\\temp\\ test.db”(在 Java 代码中转义)会起作用吗?
-
不起作用。我对所有变体都使用了双引号。
-
我刚刚重新加载了我的旧 VirtualMachine,在那里我使用“C://temp//test.db”启动了相同的代码,它可以工作!与此同时,我更新到 JAVA 7 并猜测这就是它不再工作的原因。我没有更改代码。我从旧虚拟机中复制了相同的代码,但在我的新虚拟机中它不起作用?