Question

Every search I take on that subject returns a lot of Resultset to CSV problems. I've already done that using openCSV in the way I'm pasting below:

ResultSet rs = p1.Zapytanie(q);
CSVWriter writer = new CSVWriter(new FileWriter(katalog_tabel+"/"+nazwa+".csv"), ',' ,CSVWriter.NO_QUOTE_CHARACTER);
writer.writeAll(rs, true);
writer.close();

And it works perfectly.

But now, in the other place of my application I would really like to convert it from file back to resultset (for compatibility with existing modules within my app). Is there a way to do that? Using CSVReader's readAll() returns a List<String[]>, and after casting it to resultset it comes out as null. Is it because List<String[]> is not really that simple to cast to resultset or is the problem somewhere else?

Just in case, I'm also pasting the code in which I'm trying to do that:

File zrodlo = new File(Environment.getExternalStorageDirectory().getPath()+"/DBConnector/Projekty/"+projekt+"/Tabele/"+plik);
CSVReader reader = new CSVReader(new FileReader(zrodlo));
ResultSet rs = (ResultSet) reader.readAll();
Was it helpful?

Solution

Sorry but that is not possible with openCSV. The writer uses the result set so it can read the results of a query and put that into a file (or whatever Writer you are using) in a csv format.

The reader only returns a list of strings or, if you are using the MappingStrategy, an object with appropriate fields for each item you are writing.

For what you are doing you need to create your own class that implements the ResultSet interface and populate that with the results of the readAll.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top