,直到我升級到Java 7(我猜測) HOWTO創建從Java連接數據庫的語法我的代碼附上sqlitedsb到另一個工作得很好?我的語法使用SQL-Tool可以正常工作,但是來自JAVA的我沒有成功。附加DB SQLite中從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)
B:
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)
我嘗試不同的語法(A,B,C)爲sSQL,但毫無效果。 SQL字符串應該如何?
您有特權嗎?嘗試在不同的驅動器。 – Keerthivasan
是的,我有權限訪問。已經嘗試過。那不是問題。 –
您正在使用單個('),嘗試在文件名周圍使用雙引號(「)(也顯示在[this](http://stackoverflow.com/a/9068549/3080094)answer)。 「C:\\ temp \\ test.db」(逃避Java代碼中的轉義)將起作用嗎? – vanOekel