JoshSGman
JoshSGman

Reputation: 449

Passing clojure vec to POSTGRES IN statement (?)

I'm trying to pass an array of strings to a select statement and I keep getting the error:

 org.postgresql.util.PSQLException: Can't infer the SQL type to use for an instance of clojure.lang.PersistentVector. Use setObject() with an explicit Types value to specify the type to use.

I know that the column type is correct, it looks as if passing a vector is the culprit. What is the correct way to do this?

The sql statement is formatted like so:

"SELECT * FROM said_table WHERE item_id IN (?)"

Upvotes: 1

Views: 575

Answers (1)

Arthur Ulfeldt
Arthur Ulfeldt

Reputation: 91554

This answer assumes you are using jdbc and not korma or something like it and need to generate the sql directly instead of going through some tool:

the in directive requires you to crate one ? for each item in the list. I end up using this pattern when something else requires me to build the SQL manually:

(let [placeholders (s/join ", " (repeat (count things-go-here) "?"))
      query "SELECT * FROM said_table WHERE item_id IN (%s)"]
    (exec-raw [(format query placeholders) things-go-here] :results)
    ....)

Upvotes: 1

Related Questions