使用 sql2o ( https://github.com/aaberg/sql2o )
选择具有尾间空间的VARCHAR列(例如"some value "
)返回值为"some value"
从 mysql cli 中选择时,结果包含尾随空格
找不到任何文档如何防止这种情况发生
- 桌子:
CREATE TABLE names
(
name VARCHAR(100),
PRIMARY KEY (experiment_key, metric_name)
);
- 代码示例:
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 ??? :( ");
}