1

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);
        }
    }
zjb
  • 11
  • 1
  • Please describe how it does not work like error message, or if no error then the actual output. – joseph Aug 13 '20 at 19:35
  • This is the error message I'm getting Exception in thread "main" java.lang.NullPointerException at com.healthmarketscience.jackcess.Cursor$Id.(Cursor.java:1490) at com.healthmarketscience.jackcess.Cursor$TableScanCursor.(Cursor.java:1334) at com.healthmarketscience.jackcess.Cursor$TableScanCursor.(Cursor.java:1322) at com.healthmarketscience.jackcess.Cursor.createCursor(Cursor.java:107) at com.healthmarketscience.jackcess.ExportUtil.exportWriter(ExportUtil.java:271) at DumpQuery.main(Unknown Source) – zjb Aug 13 '20 at 20:30

2 Answers2

1

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.

jtahlborn
  • 52,909
  • 5
  • 76
  • 118
0

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();
JeroSquartini
  • 347
  • 1
  • 2
  • 11
  • Thank you for your suggestion. I should have included the imports that I use in the script import com.healthmarketscience.jackcess.query.Query; import com.healthmarketscience.jackcess.Database; import com.healthmarketscience.jackcess.ExportUtil; import com.healthmarketscience.jackcess.SimpleExportFilter; I think if I can get the query rows to a then I should be able to do what you suggested. – zjb Aug 13 '20 at 20:34
  • Okay great, try that. If you can't manage to do it, I'll help you later. Right now I'm heading off from home so I won't touch my computer for the next 2-3 hours. – JeroSquartini Aug 13 '20 at 20:38
  • @zjb Hi, I'm back home. Could you do it? – JeroSquartini Aug 13 '20 at 23:18
  • No. I have not. :( still trying – zjb Aug 14 '20 at 01:52
  • Oh, don't worry, I'm sure you'll be able to do it. Now I'm in bed and about to go to sleep but I'll help you tomorrow so you can sort this out. In the meantime, can you clarify the query you are using to access the database and how many columns and rows the ResultSet has? :) – JeroSquartini Aug 14 '20 at 02:19