如何传递一个向量列表到clojure.jdbc / insert!封闭 [英] How to pass a list of vector to clojure.jdbc/insert! in closure
本文介绍了如何传递一个向量列表到clojure.jdbc / insert!封闭的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
每个以下链接 http://clojure.github.io /java.jdbc/#clojure.java.jdbc/insert !
此函数的参数为
(insert! db-spec table col-name-vec col-val-vec & col-val-vecs :transaction? true :entities identity)
因此它接受多个向量插入作为多行。
So it accept multiple vectors to be inserts as multiple rows.
但是如果我有一个向量列表,并且如何传递它到这个函数?
But if I have a list of vectors, and how to pass it to this function?
(def rows (repeat 10 [ 1 2]))
(clojure.jdbc/insert mydb test_table [:a :b] rows) ;; this doesn't work as rows is a list of vector
;; I could use map to insert one by one but that's slow,
;; so how to convert rows into multiple vector?
;;I am think of apply function but don't know how to use it here.
推荐答案
我想你要使用 apply
:
(def rows (repeat 10 [ 1 2]))
(apply clojure.jdbc/insert! mydb test_table [:a :b] rows)
apply
在您包含要作为参数传递给函数的值的集合的情况下非常有用。
In general, apply
is useful in cases where you have a collection containing values you want to pass as parameters to a function.
这篇关于如何传递一个向量列表到clojure.jdbc / insert!封闭的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文