n00b

I am a total Clojure n00b. But I am having a lot of fun learning it. It’s not like anything else I have learned to date.

I hadn’t really done much with it, other than watch presentations, mess around in the REPL, and with the help of Hexmode, get Clojure working with Slime in Emacs.

Yesterday and this morning during stolen minutes of solitude, I have been trying to get it to talk to a MySQL DB. Mostly, because I’d love to get to a point where I can, you know, actually do something with it :)

So this, for my fellow newbs that stumble here via google, is what I did.

For the purposes of demonstration, I created a 1 table db and gave it some lame data.

$ mysqladmin -uroot create hack
$ echo "CREATE TABLE hack(id INT NOT NULL AUTO_INCREMENT PRIMARY KEY);" | mysql -u root hack
$ for i in $(seq 4);do echo "use hack; INSERT INTO hack VALUES()" | mysql -u root hack;done

I can assure you the following code, is likely not best practices, but I have to start somewhere. So I whipped this together by looking at the test.clj for the clojure.contrib.sql library on github (Open Source rocks like that). And a lot of googling. The comments are there for your benefit.

(comment Import the Clojure Contrib SQL Library)
(require '[clojure.contrib.sql :as ccsql])

(comment Set up the connection string)
(def db { :classname "com.mysql.jdbc.Driver"
:subprotocol "mysql"
:subname "//localhost/hack"
:user "root"
:create true })

(comment Define a function that does a SELECT * on whatever table you pass in)
(defn get-star
"Print all the rows in the table"
[table]
(ccsql/with-connection db
(ccsql/with-query-results res
[(str "SELECT * FROM " table)]
(doseq [rec res]
(println rec)))))

(comment Execute that function)
(get-star "hack")

Then to run that code, you have to add the mysql Driver jar to your classpath. (Obviously, manually doing all this won’t scale, and after a jar or two, you’ll want to build an executable jar of your own and adopt a build process, but for the purposes of demonstration, this is fine. In fact, I have my clojure script in /bin, so I don’t actually call this this way…but whatever)


$ java -cp "/opt/clojure/clojure.jar:/opt/clojure/clojure-contrib.jar:/opt/jars/mysql-connector-java-5.1.8-bin.jar" clojure.main test.clj
{:id 1}
{:id 2}
{:id 3}
{:id 4}

About Nathan Powell

I am a middle aged technologist freak-ball.
This entry was posted in programming. Bookmark the permalink.

3 Responses to n00b

  1. Bob Igo says:

    I see the makers of clojure saw that python has almost no syntax, so why not make a language that is almost entirely syntax? :) It’s actually good to see Lisp alive and kicking; I coded in Lisp for years when I first started working – it used to be the favored language of machine translation researchers.

  2. Not sure I follow. Python definitely has a bit less visual clutter, but I think it actually has more syntax (at least as far as the parser is concerned).

  3. Bob Igo says:

    I guess I should have said “markup” and not “syntax” but I couldn’t think of the term at the time :)

Leave a Reply

Your email address will not be published. Required fields are marked *

*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>