简体   繁体   English

Clojure.contrib.sql:如何将select语句的结果检索到可以随时访问的向量中?

[英]Clojure.contrib.sql: How do I retrieve the results of a select statement into a vector that can be accessed anytime?

For example, in this query: 例如,在此查询中:

(Clojure.contrib.sql/with-query-results rows ["SELECT count(*) from tableName"] (doseq [record rows] (println (vals record))))

It seems that record and rows don't exist outside this scope but I want it to exist anytime for me access. 似乎该范围之外不存在记录和行,但是我希望它可以随时存在以供我访问。

Update: I tried the following lines of code 更新:我尝试了以下代码行

(def asdf [])
(sql/with-connection db

(sql/with-query-results rows ["SELECT * FROM tableName"] (doseq [record rows] (def asdf (conj asdf record)))))

(println asdf)

Why does the print statement of asdf above return empty when I added the rows to it in the sql statement? 当我在sql语句中向其添加行时,为什么上述asdf的print语句返回空?

You seem to lack a basic understanding of Clojure's underlying principles, in particular immutable data structures. 您似乎对Clojure的基本原理缺乏基本的了解,尤其是不可变的数据结构。 Your conj call doesn't modify the var asdf -- a var is not a variable. 您的conj调用不会修改var asdf - var不是变量。 You could do something like the following (untested) 您可以执行以下操作(未经测试)

(def asdf 
    (sql/with-connection db
         (doall (sql/with-query-results rows ["SELECT * FROM tableName"]))))

to store the result directly as the value of asdf , but this is probably not really what you want. 将结果直接存储为asdf的值,但这可能并不是您真正想要的。 Get familiar with Clojure's take on functional programming instead. 相反,请熟悉Clojure的函数式编程

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM