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