处理DB查询结果集时抛出错误 [英] Error Thrown While Processing DB Query Resultset
问题描述
任何人都可以帮助我理解我的代码(见下文)为什么导致以下错误:
Can anyone help me understand why my code (see below) is resulting in the following error:
java.lang.UnsupportedOperationException:
nth不支持此类型:PersistentStructMap
Exception in thread "main" java.lang.UnsupportedOperationException: nth not supported on this type: PersistentStructMap
(defn search [query]
(with-connection db
(with-query-results rs [query]
(doseq [[k v] rs]
(println v)))))
(search (nth *command-line-args* 0))
推荐答案
rs
是一个序列(列表),表示结果集中的所有记录。 rs
的每个元素都是一个表示单个记录的哈希表,地图中的键/值对表示该记录的字段名称和值。您尝试执行的操作相当于:
rs
is a sequence (list), representing all the records in your resultset. Each element of rs
is a hashmap representing a single record, with key/value pairs in the map representing field names and values for that record. You're trying to do the equivalent of this:
user> (let [rs [{:id 1 :val "foo"} {:id 2 :val "bar"}]]
(doseq [[k v] rs]
(println v)))
; Evaluation aborted.
; nth not supported on this type: PersistentArrayMap
这试图将每个映射解构为 [kv]
,大致相当于:
This is trying to destructure each map into [k v]
, doing the rough equivalent of this:
user> (let [k (nth {:id 1 :val "foo"} 0)
v (nth {:id 1 :val "foo"} 1)])
; Evaluation aborted.
; nth not supported on this type: PersistentArrayMap
如果您尝试打印每个字段的值每个记录,您需要这样做:
If you're trying to print the value for every field in every record, you need to do this:
user> (let [rs [{:id 1 :val "foo"} {:id 2 :val "bar"}]]
(doseq [record rs
[k v] record]
(println v)))
foo
1
bar
2
$ b b
对于结果集中的每个记录,对于该记录中的每个键/值,打印该值。
"For each record in the resultset, for each key/value in that record, print the value."
如果结果集只包含一个记录或者你只关心其中一个),并且你试图遍历该记录的字段,然后通过 doseq
只有第一
:
If your resultset contains only a single record (or you only care about one of them) and you're trying to iterate over the fields of that single record, then pass doseq
only the first
:
user> (let [rs [{:id 1 :val "foo"}]]
(doseq [[k v] (first rs)]
(println v)))
foo
1
对于结果集的第一个记录中的每个键/值,请打印该值。
"For every key/value in the first record of the resultset, print the value."
这篇关于处理DB查询结果集时抛出错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!