Open Murilovisque opened 6 years ago
Can you post a sample code? I'm quite sure binding works with inserts and updates too. I think we are using that in production code.
Table (SQLite):
CREATE TABLE config(
id INTEGER PRIMARY KEY AUTOINCREMENT,
fieldA TEXT,
field_b TEXT
);
Model:
public class Config {
private String fieldA;
private String fieldB;
//Getters and Constructor
}
Example 1:
public static void main(String[] args) {
Sql2o sql2o = new Sql2o("jdbc:sqlite:/tmp/test.db", null, null);
try (Connection conn = sql2o.open()) {
String insert = "insert into config (fieldA) values (:fieldA)";
conn.createQuery(insert).bind(new Config("a", "b"))
.executeUpdate();
}
}
It works, the class's field "fieldA" matches with table's column. But the example 2 does not work.
Example 2:
public static void main(String[] args) {
Sql2o sql2o = new Sql2o("jdbc:sqlite:/tmp/test.db", null, null);
Map<String, String> mapColumn = new HashMap<>();
mapColumn.put("field_b", "fieldB");
sql2o.setDefaultColumnMappings(mapColumn); //trying to map the columns
try (Connection conn = sql2o.open()) {
String insert = "insert into config (field_b) values (:field_b)";
conn.createQuery(insert).bind(new Config("a", "b"))
.addColumnMapping("field_b", "fieldB") //trying to map the columns
.executeUpdate();
}
}
I tried to map the column "field_b" to field "fieldB". But it throws the exception bellow.
Caused by: java.sql.SQLException: Values not bound to statement
at org.sqlite.core.CorePreparedStatement.checkParameters(CorePreparedStatement.java:69)
at org.sqlite.jdbc3.JDBC3PreparedStatement.executeUpdate(JDBC3PreparedStatement.java:100)
at org.sql2o.Query.executeUpdate(Query.java:656)
I tried execute a insert statement with binded object, I mapped some property to its column, but the error continue (org.sql2o.Sql2oException: Error in executeUpdate, Values not bound to statement). Apparently columns mapping is only used in select statement. Is there some impediment to use it in insert/update too?
conn.createQuery(INSERT).bind(config).executeUpdate().getKey()