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

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

Output Access DB query data to a csv file using Java

问题

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

  1. 我能够打开数据库文件并获取查询名称和语句如何将查询数据输出到 CSV我认为下面的 exportWriter 应该可以实现但它不起作用
  2. Database db = Database.open(new File(args[0]));
  3. for(Query query : db.getQueries()) {
  4. System.out.println(query.getName() + ": " + query.toSQLString());
  5. if (query.getName().equals("thequerytooutput")) {
  6. BufferedWriter csvOut = new BufferedWriter(new OutputStreamWriter(System.out));
  7. ExportUtil.exportWriter(db, query.getName(), csvOut, true, null, "\"",
  8. SimpleExportFilter.INSTANCE);
  9. }
  10. }

请注意,我已经修正了代码中的一个错误,将 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.

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

答案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

  1. List<List<String>> rows = Arrays.asList(
  2. Arrays.asList("Jean", "author", "Java"),
  3. Arrays.asList("David", "editor", "Python"),
  4. Arrays.asList("Scott", "editor", "Node.js")
  5. );
  6. FileWriter csvWriter = new FileWriter("new.csv");
  7. csvWriter.append("Name");
  8. csvWriter.append(",");
  9. csvWriter.append("Role");
  10. csvWriter.append(",");
  11. csvWriter.append("Topic");
  12. csvWriter.append("\n");
  13. for (List<String> rowData : rows) {
  14. csvWriter.append(String.join(",", rowData));
  15. csvWriter.append("\n");
  16. }
  17. csvWriter.flush();
  18. 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

  1. List&lt;List&lt;String&gt;&gt; rows = Arrays.asList(
  2. Arrays.asList(&quot;Jean&quot;, &quot;author&quot;, &quot;Java&quot;),
  3. Arrays.asList(&quot;David&quot;, &quot;editor&quot;, &quot;Python&quot;),
  4. Arrays.asList(&quot;Scott&quot;, &quot;editor&quot;, &quot;Node.js&quot;)
  5. );
  6. FileWriter csvWriter = new FileWriter(&quot;new.csv&quot;);
  7. csvWriter.append(&quot;Name&quot;);
  8. csvWriter.append(&quot;,&quot;);
  9. csvWriter.append(&quot;Role&quot;);
  10. csvWriter.append(&quot;,&quot;);
  11. csvWriter.append(&quot;Topic&quot;);
  12. csvWriter.append(&quot;\n&quot;);
  13. for (List&lt;String&gt; rowData : rows) {
  14. csvWriter.append(String.join(&quot;,&quot;, rowData));
  15. csvWriter.append(&quot;\n&quot;);
  16. }
  17. csvWriter.flush();
  18. 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:

确定