从Java中附加SQLite中的DB

时间:2014-01-18 11:27:54

标签: java sql sqlite

我的代码将sqlitedsb附加到另一个工作正常,直到我升级到JAVA 7(我的猜测) 如何创建从java附加数据库的语法?我的语法使用SQL-Tool工作正常,但是从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();
        }
    }
}

错误讯息:

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)

enter image description here

我为sSQL尝试了不同的语法(A,B,C),但没有任何效果。 SQL-String应该如何?

2 个答案:

答案 0 :(得分:0)

在Java字符串中,\是转义字符。 \t是水平制表符控件字符,您可以在消息中看到:

Attach 'C:  emp est.db' as chronica_maps_tests

正确的语法是这样的:

String sDatabasetoattach = "C:\\temp\\test.db";

答案 1 :(得分:0)

我为SQLite添加了一个较新的JDBC驱动程序,但它确实有效。我猜驱动程序与JAVA 7无法正常工作。我的旧JDBC驱动程序是从2011年开始的。