如果引用的列分隔符是第一个列条目,那么该行的最后一个列条目将会读取到下一行。
给定使用以下内容创建的2列文本表:
statement.executeUpdate("CREATE TEXT TABLE " + archiveName + " ("
+ "message varchar(1000),"
+ "line varchar(1000))");
...使用csv文件:
",","col 2 line 1"
"col 1 line 2","col 2 line 2"
它将第1行读作:
Col1:“,”
Col2:“col 2 line 1
“col 1 line 2”“
根本不会读取第2行。预期的行为是:
线路1:
Col1:“,”
Col2:“col 2 line 1”
线路2:
Col1:“col 1 line 2”
Col2:“col 2 line 2”
奇怪的是,如果你在引用的分隔符和line1 col1的结束引号之间加一个空格,它将正确读取文件:
", ","col 2 line 1"
"col 1 line 2","col 2 line 2"
要重现创建提到的csv文件并运行它:
public void schemaCheck() {
final String archiveName = "test";
Connection connection;
try {
connection = DriverManager.getConnection("jdbc:hsqldb:file:test", "SA", "");
try (Statement statement = connection.createStatement()) {
statement.executeUpdate("DROP TABLE IF EXISTS " + archiveName);
statement.executeUpdate("CREATE TEXT TABLE " + archiveName + " ("
+ "message varchar(1000),"
+ "line varchar(1000))");
statement.executeUpdate("SET TABLE " + archiveName + " SOURCE 'archive/" + archiveName + ".csv;encoding=UTF-8'");
} catch (SQLException e) {
throw new IllegalStateException(e);
}
try (PreparedStatement statement = connection
.prepareStatement("SELECT * FROM " + archiveName)) {
ResultSet result = statement.executeQuery();
while (result.next()) {
System.out.println("Line:");
System.out.println("First col:");
System.out.println(result.getString(1));
System.out.println("Second col:");
System.out.println(result.getString(2));
}
} catch (SQLException e) {
throw new IllegalStateException(e);
}
} catch (SQLException e1) {
throw new IllegalStateException(e1);
}
}
这是使用HSQLDB v2.4.0
我尝试过的事情:
这些都导致相同的结果:超读最后一列。除了在引用字段分隔符和结束引号之间添加空格之外,唯一有效的方法是确保所讨论的列不是第一列。
答案 0 :(得分:0)
您需要在文本来源设置中指定all_quoted:
"SET TABLE " + archiveName + " SOURCE 'archive/" + archiveName + ".csv;encoding=UTF-8;all_quoted=true'"
更新:此问题已经过检查,发现是一个错误。它已针对HSQLDB版本2.4.1进行了修复