【问题标题】:Simple Database storage in clojureclojure 中的简单数据库存储
【发布时间】:2013-02-06 13:49:44
【问题描述】:

这是一个新手问题。我有一个解析网页并返回一系列 5 个元素的函数。然后我使用println 函数来查看它是否正常工作。

...
(defn select-first-index-page-elements [source element n]
    ((get-parsing-logic source "parsing-logic-index-page" element "final-touch-fn")
        (nth 
            (html/select 
                (fetch-first-page source)
                (get-parsing-logic source "parsing-logic-index-page" element "first-touch"))
            n)))

(defn parsing-source [source]
(loop [n 0]
    (when (< n (count-first-index-page-elements source "title"))
(println ; the group of elements:
    (select-first-index-page-elements source "date" n)
    " - "
    (select-first-index-page-elements source "title" n)
    " - "
    (select-first-index-page-elements source "url" n)
    "\n")
(recur (inc n)))))))

(parsing-source "events-directory-website")

现在,我如何将这些元素存储到数据库中,而不是 println 函数?如果它已经在数据库中,我如何不能存储给定的一组元素? 我怎样才能只打印解析函数找到的新元素组?

【问题讨论】:

    标签: sql clojure


    【解决方案1】:

    您可能想查看SQL Korma

    使用 sql korma:

    如何将这些元素存储到数据库中?

    (insert my-elements
      (values [{:elements ("a" "b" "c")}]))
    

    如果给定的一组元素已经在数据库中,我怎么不能存储它?

    ;; using some elements youre looking for
    (if-not [is-in-db (select my-elements
                              (where {:elements the-elements-youre-looking-for}))]
      (insert my-elements
          (values [{:elements the-elements-youre-looking-for}])))
    

    我怎样才能只打印解析函数找到的新元素组? 您可以使用上述答案中的 (select ...) 调用来解决此问题。

    希望对您有所帮助。

    【讨论】:

    • 我得到CannotAcquireResourceException A ResourcePool could not acquire a resource from its primary factory or source. com.mchange.v2.resourcepool.BasicResourcePool.awaitAvailable (BasicResourcePool.java:1319) 而不是(println.. 我输入:(let [next-url (select-first-index-page-elements source "url" n)] (if-not [db (select events (where {:url next-url}))] (let [next-date (select-first-index-page-elements source "date" n) next-title (select-first-index-page-elements source "title" n)] (insert events (values [{:date next-date :title next-title :url next-url}])))))
    • 也许结帐this。确保 SQL 正在运行,确保您已在代码中某处声明数据库,例如 here
    • 另外,最简单的db声明示例可能是“生成查询示例:”下的here,其中使用了defdb
    猜你喜欢
    • 2010-10-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-04-10
    • 1970-01-01
    • 1970-01-01
    • 2011-05-05
    • 1970-01-01
    相关资源
    最近更新 更多