Some Practical Clojure Examples
← prev | next →     Top-level ToC     /sqlite-db.html     (printable version)

clojure/java.jdbc wraps Java’s JDBC. sqlite-jdbc is for the JVM; it has nothing in particular to do with Clojure.

#!/usr/bin/env inlein

'{:dependencies [[org.clojure/clojure    "1.8.0"]
                 [org.clojure/java.jdbc  "0.7.0-alpha1"]
                 [org.xerial/sqlite-jdbc "3.16.1"]]}

(require '[clojure.java.jdbc :as jdbc])

(def db-spec {:dbtype "sqlite"
              :dbname "foo.db"})

(defn main
  []
  (let [res (jdbc/query db-spec
                        ["select id, name, age from people"])]
    (prn res)))

;;----
(main)

Output is a seq of maps, like:

({:id 1, :name "Abe", :age 42.0}
 {:id 2, :name "Ben", :age 31.5}
 {:id 3, :name "Cal", :age 22.6})

DB column names are automatically converted to keywords.