user3745942
user3745942

Reputation: 193

Clojure: how do I print the results of an SQL query?

Let's say I want to execute the following queries: (select * from table) (select count(*) from table)

http://clojuredocs.org/clojure_contrib/clojure.contrib.sql/with-query-results

How do I put all these into a vector so that I can print out the results? Give specific examples please.

Upvotes: 1

Views: 833

Answers (1)

Sean Corfield
Sean Corfield

Reputation: 6666

The clojure.contrib.sql library is deprecated and the documentation you linked to is outdated for the current version of the library, now called clojure.java.jdbc. Updated documentation for that library can be found here:

To answer your specific question, you would want to use the query function now and you'll find detailed documentation on running queries on the community maintained site. What you get back from query is a fully-realized sequence of maps, which solves the problem I think you're running into (that with-query-result returns a lazy sequence and so if you don't fully realize it inside that macro call, you get an exception about operating on a closed connection).

The query function can process the result set in more flexible ways via :as-arrays?, :row-fn, and :result-set-fn which make it a lot easier to work with than the old library you mentioned.

Upvotes: 5

Related Questions