Menu

#92 Writing a ResultSet Should Use Column Aliases

v1.0 (example)
closed-fixed
None
5
2015-04-11
2012-11-30
No

The CSVWriter.writeAll(ResultSet rs) method works great for my needs except that it does not honor column name aliases. When writing my SQL statement I specified column aliases as in "SELECT column_name AS column_alias FROM table_name" but CSVWriter is ignoring that alias when writing the CSV file's header row.

The fix for this is a minor change to line 47 of ResultSetHelperService:
names.add(metadata.getColumnName(i+1));
should be
names.add(metadata.getColumnLabel(i+1));

This will work regardless of whether or not the user specifies column aliases because if no alias is specified, getColumnLabel() returns the column name instead.

Discussion

  • Florian Beese

    Florian Beese - 2015-04-10

    I have the same problem and would suggest the same solution as written in the ticket. I programmed a similar library once for C++ and we also decided to use the label instead of the column name.

    In my case the result is mixed up, as I have a lot of SQL expressions in my statement and for those the aliases work. So half of my columns are speaking, the others are not.

    Hope you consider fixing this or extending the interface, so that I one can easily use aliases.

    Thanks for this easy and nice lib.

     

    Last edit: Florian Beese 2015-04-10
  • Scott Conway

    Scott Conway - 2015-04-11

    Sorry this one came in while I was on hiatus from the project. I have applied the suggested fix and you can check out the trunk code to test it out. Because of family and work commitments I am not expecting to push out a new version of opencsv until June at the earliest.

     
  • Scott Conway

    Scott Conway - 2015-04-11
    • status: open --> closed-fixed
    • assigned_to: Scott Conway
    • Group: --> v1.0 (example)
     

Log in to post a comment.