Mercurial > coderloop
comparison src/hello_sql.clj @ 0:307a81e46071 tip
initial committ
author | Robert McIntyre <rlm@mit.edu> |
---|---|
date | Tue, 18 Oct 2011 01:17:49 -0700 |
parents | |
children |
comparison
equal
deleted
inserted
replaced
-1:000000000000 | 0:307a81e46071 |
---|---|
1 (ns coderloop.hello-sql | |
2 (:refer-clojure :only []) | |
3 (:require rlm.ns-rlm rlm.light-base)) | |
4 (rlm.ns-rlm/ns-clone rlm.light-base) | |
5 (use 'coderloop.utils) | |
6 | |
7 (use 'clojure.contrib.sql) ;;' satisfy prettify | |
8 (import 'org.gjt.mm.mysql.Driver) | |
9 | |
10 (def *port* 3306) | |
11 | |
12 (def *host* "localhost") | |
13 | |
14 (defn doit [] | |
15 (let [db-host "localhost" | |
16 db-port *port* | |
17 db-name "testdb"] | |
18 (def db {:classname "com.mysql.jdbc.Driver" | |
19 :subprotocol "mysql" | |
20 :subname (str "//" db-host ":" db-port "/" db-name) | |
21 :user "root" | |
22 :password "sql1005025"}) | |
23 (with-connection db | |
24 (with-query-results rs ["select * from hello_data"] | |
25 (println rs))))) | |
26 | |
27 | |
28 (defn read-hello-data [[username password db-name]] | |
29 (let [db-host *host* | |
30 db-port *port*] | |
31 (def db {:classname "com.mysql.jdbc.Driver" | |
32 :subprotocol "mysql" | |
33 :subname (str "//" db-host ":" db-port "/" db-name) | |
34 :user username | |
35 :password password}) | |
36 (with-connection db | |
37 (with-query-results rs ["select * from hello_data"] | |
38 (vec rs))))) | |
39 | |
40 | |
41 | |
42 (defn print-result [args] | |
43 (let [results (read-hello-data args)] | |
44 (println | |
45 (apply str (interpose " " (map :text results)))))) | |
46 | |
47 | |
48 (defn slick-test [] (read-hello-data ["root" "sql1005025" "testdb"])) | |
49 | |
50 | |
51 (if (command-line?) | |
52 (print-result *command-line-args*)) | |
53 | |
54 ;; rs will be a sequence of maps, | |
55 ;; one for each record in the result set. | |
56 |