我在jdbc、compojure、cheshire、postgresql、c3p0、tryin中使用clojure。当我使用这段代码作为处理程序时
(defn get-document [id]
(sql/query (db-connection)
["select * from document where id = cast(? as integer)" id]
{:row-fn
(fn [first]
(if (empty? first )
(response "empty")
(response first)
))}))如果reslutset不是空的,我有我需要的响应,但是如果它是空的,我得到空括号[]。
此外,这也是我的项目依赖项。
:dependencies [[org.clojure/clojure "1.8.0"]
[compojure "1.5.1"]
[ring/ring-json "0.4.0"]
[c3p0/c3p0 "0.9.1.2"]
[ring/ring-defaults "0.2.1"]
[org.clojure/java.jdbc "0.7.3"]
[org.postgresql/postgresql "42.1.4"]
[cheshire "5.8.0"]]发布于 2017-10-12 09:52:35
对结果集中的每一行执行:row-fn函数。当结果集为空时,将不调用row-fn。
您可能需要使用sql/query的结果来处理查询的响应。当没有结果时,返回一个空向量(这是[])。您可以通过调用empty?来检查结果是否为空。就像这样:
(defn get-document [id]
(let [query ["select * from document where id = cast(? as integer)" id]
rows (sql/query (db-connection) query)]
(if (empty? rows)
(response "empty")
(response (first rows)))))https://stackoverflow.com/questions/46705938
复制相似问题