假设您的计算机已经安装了Clojure和MySQL。
如何使它们通信?
假设您已经在您的电脑上安装了 Clojure 和 MySQL。
checkout and build clojure-contrib:
git clone git://github.com/richhickey/clojure-contrib.git
cd clojure-contrib
build
Put the resulting clojure-contrib.jar on your CLASSPATH.
Download MySQL Connector/J and put the mysql-connector-java-5.1.7-bin.jar on your CLASSPATH
You might have to run your JVM with these arguments:
-Djdbc.drivers=com.mysql.jdbc.Driver
Determine the connection URL of your MySQL database
For example, if you are running MySQL under MAMP then the URL that you would use in JDBC will look something like:
conn = DriverManager.getConnection
("jdbc:mysql://localhost:8889/db_name?user=root&password=root")
The url is broken down into these components:
jdbc:
mysql
localhost
8889
Make this clojure script, modify the database connection parameters to match your URL, save as test.clj, compile and run.
(use 'clojure.contrib.sql) ;;' satisfy prettify
(let [db-host "localhost"
db-port 8889
db-name "db_name"]
(def db {:classname "com.mysql.jdbc.Driver"
:subprotocol "mysql"
:subname (str "//" db-host ":" db-port "/" db-name)
:user "root"
:password "root"})
(with-connection db
(with-query-results rs ["select * from languages"]
(dorun (map #(println (:language :iso_code %)) rs)))))
; rs will be a sequence of maps,
; one for each record in the result set.
注意:这段代码是基于Mark Volkmann编写的类似代码进行了修改,以便从Clojure中访问Postgres数据库。
以下是一份友好的Lein配置,大部分内容参考了Nurullah Akkaya的这篇博客:
在你的project.clj
文件中添加依赖项:
(defproject clojql "1.0.0-SNAPSHOT"
:description "FIXME: write description"
:dependencies [[org.clojure/clojure "1.2.1"]
[org.clojure/clojure-contrib "1.2.0"] ;; for clojure.contrib.sql
[org.clojure/java.jdbc "0.0.6"] ;; jdbc
[mysql/mysql-connector-java "5.1.6"]]) ;; mysql driver
在命令行中运行lein deps
来获取依赖项
在地图中指定您的连接信息:
user=> (use 'clojure.contrib.sql)
nil
user=> (def db {:classname "com.mysql.jdbc.Driver"
:subprotocol "mysql"
:subname "//localhost:3306/nmr"
:user "root"})
使用with-connection
和with-query-results
宏(及其他):user=> (with-connection db (with-query-results rs ["select * from sometable"] (count rs)))
667
第3步和第4步可以在REPL中执行(使用lein repl
启动),也可以在普通源代码中执行
:dependencies [[org.clojure/clojure "1.8.0"]
[org.clojure/java.jdbc "0.4.2"]
[mysql/mysql-connector-java "5.1.38"]]
在命名空间定义内部需要引入数据库连接器:
(ns name.space
(:require [clojure.java.jdbc :as j]))
(def db-map {:subprotocol "mysql"
:subname "//localhost:3306/SCHEME"
:user "DB_USER"
:password "DB_USER_PASS"})
(j/query db-map ["SELECT * FROM table"])
在http://clojure-doc.org/articles/ecosystem/java_jdbc/using_sql.html中查找更多的示例。
(use 'korma.db)
(defdb db (postgres {:db "mydb"
:user "user"
:password "dbpass"}))
(use 'korma.core)
(defentity users)
(select users)
;; executes: SELECT * FROM users
(select users
(where (or (= :usersname "chris")
(= :email "chris@chris.com"))))
;; executes: SELECT * FROM users WHERE (users.usersname = 'chris' OR users.email = 'chris@chris.com')
defdb
确实指向Postgres,但文档涵盖了除MySQL之外的其他数据库。请参见此处:http://sqlkorma.com/docs#db
也许可以更新代码片段以更好地与问题相关联。 - Ron Klein