sql2o - select query removing trailing spaces from VARCHAR coulmn

254 Views Asked by At

using sql2o (https://github.com/aaberg/sql2o)

when selecting a VARCHAR column that has trailing spaces (for example "some value ") the return value is "some value"

when selecting from mysql cli the result contains the trailing spaces

cant find any documentation how to prevent this from happening

  • table:
CREATE TABLE names
(
    name     VARCHAR(100),
    PRIMARY KEY (experiment_key, metric_name)
);
  • code example:
 Sql2o sql2o;

        String name = "some name with trailing space    ";
        try (Connection con = sql2o.open()) {
            con.createQuery("INSERT INTO names (name) VALUES(:name)")
                    .addParameter("name", name)
                    .executeUpdate();
        }        


        String nameFromDB; 
        try (Connection con = sql2o.open()) {
            nameFromDB =  con.createQuery("select name from names")
                    .executeAndFetchFirst(String.class);
        }

        if (!nameFromDB.equals(name)){
            throw new RuntimeException("where did the trailing spaces go ??? :( ");
        }
1

There are 1 best solutions below

2
derek.wolfe On BEST ANSWER

Think I found your answer in Sql2o.

I believe by using String.class, it is using the StringConverter class to convert your query output into a string. At the very bottom of the StringConverter class is this line:

return val.toString().trim();

Found here