将Access数据库查询数据输出到CSV文件中,使用Java。

huangapple go评论149阅读模式
英文:

Output Access DB query data to a csv file using Java

问题

以下是翻译好的代码部分:

我能够打开数据库文件并获取查询名称和语句如何将查询数据输出到 CSV我认为下面的 exportWriter 应该可以实现但它不起作用

Database db = Database.open(new File(args[0]));    
for(Query query : db.getQueries()) {
    System.out.println(query.getName() + ": " + query.toSQLString());
    if (query.getName().equals("thequerytooutput")) {
        BufferedWriter csvOut = new BufferedWriter(new OutputStreamWriter(System.out));
        ExportUtil.exportWriter(db, query.getName(), csvOut, true, null, "\"", 
            SimpleExportFilter.INSTANCE);
    }
}

请注意,我已经修正了代码中的一个错误,将 if query.getName() = "thequerytooutput" 更正为 if (query.getName().equals("thequerytooutput"))

英文:

I am able to open the database file and get the query name and the statement. How do I output the query data to a csv? I thought he below exportWriter would do it but it doesn't work.

Database db = Database.open(new File(args[0]));    
    for(Query query : db.getQueries()) {
        System.out.println(query.getName() + ": " + query.toSQLString());
        if query.getName() = "thequerytooutput" {
            BufferedWriter csvOut = new BufferedWriter(new OutputStreamWriter(System.out));
    ExportUtil.exportWriter(db, query.getName(), csvOut, true, null, '"', 
        SimpleExportFilter.INSTANCE);
        }
    }

答案1

得分: 1

ExportUtil 不运行查询,它只转储表格。您需要将有效的表格名称作为第二个参数提供。Jackcess 无法执行 SQL 查询,如此处所述。

英文:

ExportUtil does not run queries, it only dumps tables. You need to provide a valid table name as the second parameter. Jackcess does not have the ability to execute sql queries, as noted here.

答案2

得分: 0

我认为更好的选择是首先将数据库中的值存储到一个"二维"字符串列表中(我假设您知道如何做到这一点[如果不知道,请告诉我,我会进一步说明])。然后使用FileWriter将数组写入CSV文件。

这个示例来自stackabuse.com

List<List<String>> rows = Arrays.asList(
    Arrays.asList("Jean", "author", "Java"),
    Arrays.asList("David", "editor", "Python"),
    Arrays.asList("Scott", "editor", "Node.js")
);

FileWriter csvWriter = new FileWriter("new.csv");
csvWriter.append("Name");
csvWriter.append(",");
csvWriter.append("Role");
csvWriter.append(",");
csvWriter.append("Topic");
csvWriter.append("\n");

for (List<String> rowData : rows) {
    csvWriter.append(String.join(",", rowData));
    csvWriter.append("\n");
}

csvWriter.flush();
csvWriter.close();
英文:

I think a better option would be to first store your values from the database into a "two dimensional" list of Strings( I'm assuming you know how to do that[if not, tell me and I'll clarify more]). And then use FileWriter to write the array into a CSV file.

This example was taken from stackabuse.com

List&lt;List&lt;String&gt;&gt; rows = Arrays.asList(
    Arrays.asList(&quot;Jean&quot;, &quot;author&quot;, &quot;Java&quot;),
    Arrays.asList(&quot;David&quot;, &quot;editor&quot;, &quot;Python&quot;),
    Arrays.asList(&quot;Scott&quot;, &quot;editor&quot;, &quot;Node.js&quot;)
);

FileWriter csvWriter = new FileWriter(&quot;new.csv&quot;);
csvWriter.append(&quot;Name&quot;);
csvWriter.append(&quot;,&quot;);
csvWriter.append(&quot;Role&quot;);
csvWriter.append(&quot;,&quot;);
csvWriter.append(&quot;Topic&quot;);
csvWriter.append(&quot;\n&quot;);

for (List&lt;String&gt; rowData : rows) {
    csvWriter.append(String.join(&quot;,&quot;, rowData));
    csvWriter.append(&quot;\n&quot;);
}

csvWriter.flush();
csvWriter.close();

huangapple
  • 本文由 发表于 2020年8月14日 03:14:19
  • 转载请务必保留本文链接:https://go.coder-hub.com/63401825.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定