Hi Manoj, On Wed, Jun 1, 2011 at 12:35 PM, mmwaikar <mmwai...@gmail.com> wrote:
> Hi, > > I've to use clojure.contrib.sql's insert-records fn. > > Usage: (insert-records table & records) > > Inserts records into a table. records are maps from strings or > keywords (identifying columns) to values. > > So, for ex. this works - > (clojure.contrib.sql/insert-records :blogs > {:id 3 :title "third" :body > "third post"} > {:id 4 :title "fourth" :body > "fourth post"}) > > but this doesn't - > (clojure.contrib.sql/insert-records :blogs > *(*{:id 3 :title "third" :body > "third post"} > {:id 4 :title "fourth" :body > "fourth post"}*)*) > > So, how do I retrieve individual maps from - (map #(zipmap [:a :b :c :d] %) > [[1 2 3 4] [5 6 7 8]]) [where :a :b etc. are columns and the second vector is > values] > because the above gives me back - ({:d 4, :c 3, :b 2, :a 1} {:d 8, :c 7, :b > 6, :a 5}) which I cannot pass to the "insert-records" function. > > Use apply if you need to splice a list back into individual arguments. This should work: (apply clojure.contrib.sql/insert-records :blogs '({:d 4, :c 3, :b 2, :a 1} {:d 8, :c 7, :b 6, :a 5})) > Also, I get confused as to why some functions work on [] but not on lists (). > In such cases, do I have to convert a list into a vector using something like > vec? > > Be careful with the list syntax. You usually must quote it with '(..) to prevent Clojure trying to treat it like a function call, or alternatively construct them with (list ...). This is a great example as a Map is a valid function! Ouch! user=> ({:id 3 :title "third" :body "third post"} {:id 4 :title "fourth" :body "fourth post"}) nil user=> '({:id 3 :title "third" :body "third post"} {:id 4 :title "fourth" :body "fourth post"}) ({:id 3, :title "third", :body "third post"} {:id 4, :title "fourth", :body "fourth post"}) As for lists vs vectors: It depends on the function, AFAIK there are some subtle semantic differences using lists and vectors like which order they are mapped against, so be careful. Browsing the source code of the function in interest usually answers what is it expecting. Usually being Seqable is enough. Thanks, Ambrose -- You received this message because you are subscribed to the Google Groups "Clojure" group. To post to this group, send email to clojure@googlegroups.com Note that posts from new members are moderated - please be patient with your first post. To unsubscribe from this group, send email to clojure+unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/clojure?hl=en