adrian
adrian

Reputation: 2846

ResultSet returns blank column in CSV

I'm using JOOQ and Postgres. In Postgres I have a column gender:

 'gender' AS gender,

(the table itself is a view and the gender column is a placeholder for a value that gets calculated in Java)

In Java when I .fetch() the view, I do some calculations on each record:

     for (Record r : skillRecords) {
            idNumber=function(r)
            r.set(id, idNumber);
            r.set(gender,getGender(idNumber));
        }

All looks good and if println the values they're all correct. However, when I call intoResultSet() on my skillsRecord, the gender column has an asterisks next to all the values, eg "*Male".

Then, I use the resultset as input into an OpenCSV CSV writer and when I open the CSV the gender column comes out as null.

Any suggestions?

UPDATE:

Following the input from Lukas regarding the asterisks, I realise the issue is likely with opencsv.

My code is as follows:

        File tempFile = new File("/tmp/file.csv");

        BufferedWriter out = new BufferedWriter(new FileWriter(tempFile));
        CSVWriter writer = new CSVWriter(out);
        //Code for getting records sits here
     for (Record r : skillRecords) {
        idNumber=function(r)
        r.set(id, idNumber);
        r.set(gender,getGender(idNumber));
    }
        writer.writeAll(skillRecords.intoResultSet(), true);
        return tempFile;

All the columsn in the CSV come back as expected, except the gender column, which has the header "gender" but the column values are empty. I have the necessary try/catches in the code above but I've excluded them for brevity.

Upvotes: 1

Views: 223

Answers (2)

adrian
adrian

Reputation: 2846

Solution:

So I found the solution. It turns out with postgres if I have something like:

'gender' AS gender,

The type is unknown, not text. So the solution was to define as:

'gender'::text AS gender

After doing so OpenCSV was happy.

Upvotes: 1

Lukas Eder
Lukas Eder

Reputation: 220762

The asterisk in *Male

The asterisk that you see in the ResultSet.toString() output (or in Result.toString()) reflects the record's internal Record.changed(Field) flag, i.e. the information on each record that says that the record was modified after it was retrieved form the database (which you did).

That is just visual information which you can safely ignore.

Upvotes: 1

Related Questions