My code to attach a sqlitedsb to another worked fine until I upgraded to JAVA 7 ( my guess ) Howto create the syntax for attaching the database from java ? My syntax works fine using a SQL-Tool, but from JAVA I do not succeed.
My Code :
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();
}
}
}
Error Messages:
A:
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)
I tried different syntaxes (A,B,C) for sSQL, but nothing worked. How should the SQL-String be ?