2015-04-12 119 views
1

我想使用csvJDBC執行嵌套查詢。使用CSVJDBC嵌套查詢

有問題的表具有以下屬性:

comp(comp_id, comp_description) 
work_opportunities(wo_id, jp_id) 
link_wo_comp (wo_id, comp_id) 

查詢是:

SELECT comp_id, count(comp_id) 
FROM link_wo_comp 
WHERE wo_id IN (SELECT distinct wo_id FROM work_opportunities WHERE jp_id = '1') 
GROUP BY comp_id; 

我已經進行子查詢,對自己的主查詢和他們工作。

這是代碼我使用:

try { 
      Class.forName("org.relique.jdbc.csv.CsvDriver"); 

      Connection conn = DriverManager.getConnection("jdbc:relique:csv:" 
        + "D:/Desktop/Data/Cleansed/"); 

      Statement stmt = conn.createStatement(); 

      String sql = "SELECT comp_id, count(comp_id) " 
        + "FROM link_wo_comp " 
        + "WHERE wo_id IN (SELECT distinct wo_id FROM work_opportunities WHERE jp_id = '1') " 
        + "GROUP BY comp_id;"; 

      ResultSet results = stmt.executeQuery(sql); 

      boolean append = true; 
      CsvDriver.writeToCsv(results, System.out, append); 

      // Clean up 
      conn.close(); 
     } catch (Exception e) { 
      e.printStackTrace(); 
     } 

錯誤日誌是這樣的:

java.sql.SQLException: Syntax error: Encountered "" at line 1, column 66. 
Was expecting one of: 

    at org.relique.jdbc.csv.CsvStatement.executeQuery(Unknown Source) 
    at testing.TestRun.main(TestRun.java:30) 

不csvJDBC支持嵌套查詢,如果有什麼錯我的代碼?

謝謝

回答

0

CSVJDBC可能無法在同一時間從多個CSV文件處理嵌套查詢或SELECT語句,但HSQLDB支持CSV文件作爲Text Tables所以你可以使用這樣的代碼:

public static void main(String[] args) { 
    // create HSQLDB :file: database in the same folder as the CSV files 
    String connectionUrl = "jdbc:hsqldb:file:C:/__tmp/hsqldbCSV/Cleansed/hsqldb"; 
    try (Connection conn = DriverManager.getConnection(connectionUrl, "SA", "")) { 
     try (Statement s = conn.createStatement()) { 
      s.execute("DROP TABLE IF EXISTS comp"); 
      s.execute("CREATE TEXT TABLE comp (comp_id int, comp_description varchar(50))"); 
      s.execute("SET TABLE comp SOURCE 'comp.csv'"); 
      s.execute("DROP TABLE IF EXISTS work_opportunities"); 
      s.execute("CREATE TEXT TABLE work_opportunities (wo_id int, jp_id int)"); 
      s.execute("SET TABLE work_opportunities SOURCE 'work_opportunities.csv'"); 
      s.execute("DROP TABLE IF EXISTS link_wo_comp"); 
      s.execute("CREATE TEXT TABLE link_wo_comp (wo_id int, comp_id int)"); 
      s.execute("SET TABLE link_wo_comp SOURCE 'link_wo_comp.csv'"); 
      String sql = "SELECT comp_id, count(comp_id) " 
        + "FROM link_wo_comp " 
        + "WHERE wo_id IN (SELECT distinct wo_id FROM work_opportunities WHERE jp_id = '1') " 
        + "GROUP BY comp_id;"; 
      try (ResultSet rs = s.executeQuery(sql)) { 
       while (rs.next()) { 
        System.out.println(String.format("comp_id: %d, count: %d", rs.getInt(1), rs.getInt(2))); 
       } 
      } 
      s.execute("SHUTDOWN"); 
     } 
    } catch (Exception e) { 
     e.printStackTrace(System.err); 
    } 
} 
+0

謝謝了! :) – user2415416

1

簡而言之沒有它不支持。

CsvJdbc只接受來自single表的SQL SELECT查詢,並且不支持INSERT,UPDATE,DELETE或CREATE語句。不支持SQL SELECT查詢中的表之間的連接。

請參考文檔鏈接查看更多

http://csvjdbc.sourceforge.net/doc.html

+0

b吮。謝謝:) – user2415416