英文:
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<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();
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论