Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

executeUpdate() with binded object does not check column mappings #303

Open
Murilovisque opened this issue Sep 13, 2018 · 2 comments
Open

Comments

@Murilovisque
Copy link

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()

@zsoca8711
Copy link
Contributor

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.

@Murilovisque
Copy link
Author

Murilovisque commented Sep 17, 2018

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)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants