在clojure.jdbc查询调用中使用:row-fn失败

时间:2016-07-12 23:09:05

标签: jdbc clojure

此代码有效,打印给定表中的行:

(defn count-extreferences-subset [config]
  (let [emr-dbs (:emr-databases config)]
    (println "Counting external references: " emr-dbs)
    (jdbc/with-db-connection [dbconn (:db-spec (first emr-dbs))]
                             (let [q "SELECT * FROM LOCREG"
                                    rs (jdbc/query dbconn [q])]
                               (dorun (map println rs))))))

根据clojure.jdbc中的文档,这也应该有效,但是应该在实现结果集时打印行(防止大型结果集的内存溢出):

(defn count-extreferences-subset [config]
  (let [emr-dbs (:emr-databases config)]
    (println "Counting external references: " emr-dbs)
    (jdbc/with-db-connection [dbconn (:db-spec (first emr-dbs))]
                             (let [q "SELECT * FROM LOCREG"
                                   _ (jdbc/query dbconn [q] {:row-fn println})]))))

然而,这在运行时失败,但有以下异常:

java.lang.IllegalArgumentException: No value supplied for key: {:row-fn #object[clojure.core$println 0x46ed7a0e "clojure.core$println@46ed7a0e"]}

知道使用:row-fn选项失败的原因吗?

1 个答案:

答案 0 :(得分:1)

我认为花括号是问题所在。您的代码应遵循以下模式:

(jdbc/query db-spec 
  ["select name, cost from fruit where cost = 12"]
  :row-fn add-tax)

您可以提供更多信息in The Clojure Cookbook。我强烈推荐购买一份!