Select 从H2中选择“添加”;clob:“;用clojure编辑文本
我正在使用clojure的h2数据库。我做了一张表格,像这样放了一些数据Select 从H2中选择“添加”;clob:“;用clojure编辑文本,select,text,clojure,h2,Select,Text,Clojure,H2,我正在使用clojure的h2数据库。我做了一张表格,像这样放了一些数据 (j/create-table :locations [:id "bigint primary key auto_increment"] [:title "varchar (255)"] [:part "clob"]) (j/insert-records :locations {:title "Steven Gerrard: I'm not the new Beckham" :part
(j/create-table :locations
[:id "bigint primary key auto_increment"]
[:title "varchar (255)"]
[:part "clob"])
(j/insert-records :locations
{:title "Steven Gerrard: I'm not the new Beckham" :part "He might not have the Becks appeal -- but Steven Gerrard says he's ready to light up Hollywood in his own way..."}))
)
)
然后我选择了数据
(defn newest []
(database/mysql-db)
(let [results (j/with-connection db-spec
(j/with-query-results res
["select id, title, part from locations"]
(doall res)))]
results))
我用了clostache页面上的数据
<div class="container">
<sections>
{{#newest}}
<p style="padding-bottom: 15px;">{{title}}<p>
<p style="padding-bottom: 15px;">{{part}}<p>
{{/newest}}
</sections>
</div>
如何删除附加到数据库中文本的
clob0:
字符串?即使我对数据库中的零件列使用text
而不是clob
,也会发生这种情况。我删除了clob:
:
(defn clob-to-string [row]
(assoc row :text (with-open [rdr (java.io.BufferedReader. (.getCharacterStream (:text row)))]
(apply str (line-seq rdr)))))
(defn newest []
(database/mysql-db)
(j/query db-spec
["select id, title, text from news"]
:row-fn clob-to-string
))
“clob0:”前缀实际上并没有附加到数据库中的数据 之所以会出现这种情况,是因为ResultSet为列“part”返回类
java.sql.Clob
的实例(对于Clob和TEXT数据类型,它们实际上都是)
使用
toString()
方法将Clob转换为字符串是不合适的,实现可以自由返回任何人类可读的形式。正确的方法可以在中找到 您可能已经注意到,由于上述原因,新线丢失了。。。像这样的东西又把它们加回来了
(defn clob-to-string [clob]
"Turn an Clob into a String, with new new lines"
(with-open [rdr (java.io.BufferedReader. (.getCharacterStream clob))]
(let [lseq (line-seq rdr)
butlast-line (butlast lseq)
butlast-line-mapped (map (fn [l] (str l "\n")) butlast-line)
last-line (last lseq)
all-lines-with-newline (concat butlast-line-mapped last-line)
]
(apply str all-lines-with-newline)
)))
(defn clob-to-string [clob]
"Turn an Clob into a String, with new new lines"
(with-open [rdr (java.io.BufferedReader. (.getCharacterStream clob))]
(let [lseq (line-seq rdr)
butlast-line (butlast lseq)
butlast-line-mapped (map (fn [l] (str l "\n")) butlast-line)
last-line (last lseq)
all-lines-with-newline (concat butlast-line-mapped last-line)
]
(apply str all-lines-with-newline)
)))