This page is not created by, affiliated with, or supported by Slack Technologies, Inc.
2018-01-26
Channels
- # arachne (80)
- # beginners (76)
- # boot (16)
- # cider (66)
- # cljs-dev (62)
- # cljsjs (1)
- # clojure (106)
- # clojure-dev (5)
- # clojure-greece (2)
- # clojure-italy (9)
- # clojure-russia (1)
- # clojure-spec (61)
- # clojure-uk (130)
- # clojurescript (21)
- # core-async (9)
- # cursive (3)
- # datomic (37)
- # events (41)
- # figwheel (31)
- # fulcro (27)
- # hoplon (1)
- # jobs (2)
- # lumo (11)
- # off-topic (155)
- # re-frame (71)
- # reagent (27)
- # ring-swagger (3)
- # shadow-cljs (132)
- # spacemacs (5)
- # specter (1)
- # sql (37)
- # test-check (10)
- # uncomplicate (5)
- # unrepl (2)
- # yada (3)
Hey all, quick question - has anyone had success using row value expressions in a parameterized query?
e.g.
(query ["select * from my_table where (my_table.x, my_table.y) in (values (?))" [1,2])
I had a look at the Java docs (I'm not a Java programmer) but couldn't find a type for row expressions (to extend ISQLValue in clojure.java.jdbc)
(honeysql.core/format
(-> (apply with-static :my_cte [:x :y :z] (partition 3 (range 9)))
(sql/select :*)
(sql/from :my_cte)))
["WITH my_cte (x, y, z) AS (VALUES (?, ?, ?), (?, ?, ?), (?, ?, ?)) SELECT * FROM my_cte" 0 1 2 3 4 5 6 7 8]
Im trying to run my sql queries from
(def create-tables ["Create table customers(id integer,
name varchar(60),
amount numeric(14,2))"])
(let [data (jdbc/query (@db-connections (keyword "pgdb")) [(create-tables)])]
(println data))
I keep getting this error Wrong number of args (0) passed to: PersistentVector
this works just fine
(let [data (jdbc/query (@db-connections (keyword "pgdb")) ["select * from customer"])]
(println data))
the issue is that you used def and created a vector, not a function (although a vector happens to be a function of its indexes)
so (create-tables 0) would technically work (since create-tables is a vector and vectors are functions of their indexes), however (create-tables) (vector called with no arguments) has no definition. This is why you get the seemingly weird error message.
alright so I changed the let to
(let [data (jdbc/query (@db-connections (keyword "pgdb")) (create-tables))]
(println data))
(defn create-tables ["DROP TABLE customers"])
but now i get this error Unsupported binding form: DROP TABLE customers
@jisaacs46 defn
declares functions.
You also cannot use query
with DDL (create table) -- you must use db-do-commands
.
(I feel like we've had this conversation before -- but maybe it was @josmith2016 trying to do the same thing)
(defn create-tables ["DROP TABLE customers"])
^ this should be the parameter vector for the function you are trying to define
Make sense?(although defining something called create-...
that actually DROPS a table is asking for problems 🙂 )
yeah but can I pass a function in the
[(function-name)]
instead of a ["DROP TABLE customers"]
@seancorfield is it ok to drop the tables then create them in the same function or would you recommend a different approach?
(let [data (jdbc/db-do-commands (@db-connections (keyword "pgdb")) (create-tables))]
(println data))
If create-tables
is a function that takes no arguments and returns a vector of SQL commands, that will work.
(defn create-tables ["CREATE TABLE customer(
id integer,
amount numeric(14,2),
name varchar(60))"])