diff 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
line wrap: on
line diff
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/src/hello_sql.clj	Tue Oct 18 01:17:49 2011 -0700
     1.3 @@ -0,0 +1,56 @@
     1.4 +(ns coderloop.hello-sql
     1.5 +  (:refer-clojure :only [])
     1.6 +  (:require rlm.ns-rlm rlm.light-base))
     1.7 +(rlm.ns-rlm/ns-clone rlm.light-base)
     1.8 +(use 'coderloop.utils)
     1.9 +
    1.10 +(use 'clojure.contrib.sql)               ;;' satisfy prettify
    1.11 +(import 'org.gjt.mm.mysql.Driver)
    1.12 +
    1.13 +(def *port* 3306)
    1.14 +
    1.15 +(def *host* "localhost")
    1.16 +
    1.17 +(defn doit []
    1.18 +  (let [db-host "localhost"
    1.19 +	db-port *port*
    1.20 +	db-name "testdb"]
    1.21 +    (def db {:classname "com.mysql.jdbc.Driver"
    1.22 +	     :subprotocol "mysql"
    1.23 +	     :subname (str "//" db-host ":" db-port "/" db-name)
    1.24 +	     :user "root"
    1.25 +	     :password "sql1005025"})
    1.26 +    (with-connection db
    1.27 +      (with-query-results rs ["select * from hello_data"]
    1.28 +	(println rs)))))
    1.29 +
    1.30 +
    1.31 +(defn read-hello-data [[username password db-name]]
    1.32 +  (let [db-host *host*
    1.33 +	db-port *port*]
    1.34 +    (def db {:classname "com.mysql.jdbc.Driver"
    1.35 +	     :subprotocol "mysql"
    1.36 +	     :subname (str "//" db-host ":" db-port "/" db-name)
    1.37 +	     :user username
    1.38 +	     :password password})
    1.39 +    (with-connection db
    1.40 +      (with-query-results rs ["select * from hello_data"]
    1.41 +	(vec rs)))))
    1.42 +
    1.43 +
    1.44 +
    1.45 +(defn print-result [args]
    1.46 +  (let [results (read-hello-data args)]
    1.47 +    (println
    1.48 +     (apply str (interpose " " (map :text results))))))
    1.49 +
    1.50 +
    1.51 +(defn slick-test [] (read-hello-data ["root" "sql1005025" "testdb"]))
    1.52 +
    1.53 +
    1.54 +(if (command-line?)
    1.55 +  (print-result *command-line-args*))
    1.56 +  
    1.57 +;; rs will be a sequence of maps,
    1.58 +;; one for each record in the result set.
    1.59 +