sql2o - select query removing trailing spaces from VARCHAR coulmn

Nimrod007 :

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 ??? :( ");
        }
drakin8564 :

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

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=157465&siteId=1