2013-02-06 46 views
2

這是一個新手問題。我有一個解析網頁並返回一系列5個元素的函數。然後我使用println函數來查看它是否正常工作。clojure中的簡單數據庫存儲

... 
(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功能,我怎麼會存儲這些元素融入一個DB?以及如果我已經在數據庫中不能存儲給定的元素組? 如何打印,然後只打印解析函數找到的新組元素?

回答

3

你可能想看看SQL Korma

使用sql咖喱:

我怎麼會存儲這些元素融入一個DB?

(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 ...)調用來解決這個問題。

希望有所幫助。

+0

我得到'CannotAcquireResourceException ResourcePool無法從其主工廠或源獲取資源。 com.mchange.v2.resourcepool.BasicResourcePool.awaitAvailable(BasicResourcePool.java:1319)' ,而不是'(println..'當我把: '(讓[下一URL(選擇先索引頁面元素源 「URL」 N)] (如果-不是[分貝(選擇事件(其中{:URL下一URL}))] (讓[下一個最新(選擇先索引頁元素源 「日期」 n)next-title(select-first-index-page-elements source「title」n)] (insert events(values [{:date next-date:title next-title:url next-url}]))) ))' – leontalbot

+0

也許結帳[this](http://stackoverflow.com/questions/3465872/com-mchange-v2-resourcepool-cannotacquireresource-ception-a-resourcepool-could)。確保SQL正在運行,確保你已經在代碼中聲明瞭db,如[here](http://sqlkorma.com/docs#db) – sinemetu1

+0

另外,db聲明最簡單的例子是probabl y [here](https://github.com/korma/Korma),在「使用defdb」的「生成查詢的示例:」中。 – sinemetu1