Skip to content

Instantly share code, notes, and snippets.

@martintrojer
Created August 27, 2012 14:05
Show Gist options
  • Save martintrojer/3488729 to your computer and use it in GitHub Desktop.
Save martintrojer/3488729 to your computer and use it in GitHub Desktop.
org.clojure/java.jdbc
(ns jdbc.core
(:require [clojure.java.jdbc :as jdbc]))
(def db-spec {:classname "org.sqlite.JDBC"
:subprotocol "sqlite"
:subname "test.db"})
(jdbc/with-connection db-spec
(jdbc/create-table :authors
[:id "integer primary key"]
[:first_name "varchar"]
[:last_name "varchar"]))
(jdbc/with-connection db-spec
(jdbc/insert-records :authors
{:first_name "Chas" :last_name "Emerick"}
{:first_name "Christophe" :last_name "Grand"}
{:first_name "Brian" :last_name "Carper"}))
(jdbc/with-connection db-spec
(jdbc/with-query-results res ["SELECT * FROM authors"]
(doall res)))
(jdbc/with-connection db-spec
(jdbc/with-query-results res ["SELECT * FROM authors WHERE id = ?" 2]
(doall res)))
(jdbc/with-connection db-spec (jdbc/transaction
(jdbc/delete-rows :authors ["id = ?" 1])
(throw (Exception. "Abort transaction!"))))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment