Question

I'm using "Web development with Clojure" by Dmitri Sotnikov to learn how to build web application. When I tried to run some codes for database accessing some compiling errors happend:

After running

>lein ring server

I got:

Compiling tonglindb.models.db
Exception in thread "main" java.lang.RuntimeException: No such var: sql/create-table, compiling:(tonglindb/models/db.clj:10:3)
    at clojure.lang.Compiler.analyze(Compiler.java:6380)
    at clojure.lang.Compiler.analyze(Compiler.java:6322)
    at clojure.lang.Compiler$InvokeExpr.parse(Compiler.java:3573)
    at clojure.lang.Compiler.analyzeSeq(Compiler.java:6562)
    at clojure.lang.Compiler.analyze(Compiler.java:6361)
    at clojure.lang.Compiler.analyze(Compiler.java:6322)
    at clojure.lang.Compiler$BodyExpr$Parser.parse(Compiler.java:5708)
    at clojure.lang.Compiler$FnMethod.parse(Compiler.java:5139)
    at clojure.lang.Compiler$FnExpr.parse(Compiler.java:3751)
    at clojure.lang.Compiler.analyzeSeq(Compiler.java:6558)
    at clojure.lang.Compiler.analyze(Compiler.java:6361)
    at clojure.lang.Compiler.analyzeSeq(Compiler.java:6548)
    at clojure.lang.Compiler.analyze(Compiler.java:6361)
    at clojure.lang.Compiler.access$100(Compiler.java:37)
    at clojure.lang.Compiler$DefExpr$Parser.parse(Compiler.java:529)
    at clojure.lang.Compiler.analyzeSeq(Compiler.java:6560)
    at clojure.lang.Compiler.analyze(Compiler.java:6361)
    at clojure.lang.Compiler.analyze(Compiler.java:6322)
    at clojure.lang.Compiler.compile1(Compiler.java:7148)
    at clojure.lang.Compiler.compile(Compiler.java:7219)
    at clojure.lang.RT.compile(RT.java:398)
    at clojure.lang.RT.load(RT.java:438)
    at clojure.lang.RT.load(RT.java:411)
    at clojure.core$load$fn__5018.invoke(core.clj:5530)
    at clojure.core$load.doInvoke(core.clj:5529)
    at clojure.lang.RestFn.invoke(RestFn.java:408)
    at clojure.core$load_one.invoke(core.clj:5336)
    at clojure.core$compile$fn__5023.invoke(core.clj:5541)
    at clojure.core$compile.invoke(core.clj:5540)
    at user$eval9.invoke(form-init2674485681777655551.clj:1)
    at clojure.lang.Compiler.eval(Compiler.java:6619)
    at clojure.lang.Compiler.eval(Compiler.java:6609)
    at clojure.lang.Compiler.load(Compiler.java:7064)
    at clojure.lang.Compiler.loadFile(Compiler.java:7020)
    at clojure.main$load_script.invoke(main.clj:294)
    at clojure.main$init_opt.invoke(main.clj:299)
    at clojure.main$initialize.invoke(main.clj:327)
    at clojure.main$null_opt.invoke(main.clj:362)
    at clojure.main$main.doInvoke(main.clj:440)
    at clojure.lang.RestFn.invoke(RestFn.java:421)
    at clojure.lang.Var.invoke(Var.java:419)
    at clojure.lang.AFn.applyToHelper(AFn.java:163)
    at clojure.lang.Var.applyTo(Var.java:532)
    at clojure.main.main(main.java:37)
Caused by: java.lang.RuntimeException: No such var: sql/create-table
    at clojure.lang.Util.runtimeException(Util.java:219)
    at clojure.lang.Compiler.resolveIn(Compiler.java:6848)
    at clojure.lang.Compiler.resolve(Compiler.java:6818)
    at clojure.lang.Compiler.analyzeSymbol(Compiler.java:6779)
    at clojure.lang.Compiler.analyze(Compiler.java:6343)
    ... 43 more
Compilation failed: Subprocess failed

(Before that I runned lein deps, it returned nothing. I think that means there is no dependencies problem.)

Here is my project.clj:

(defproject tonglindb "0.1.0-SNAPSHOT"
  :description "FIXME: write description"
  :url "http://example.com/FIXME"
  :dependencies [[org.clojure/clojure "1.5.1"]
                 [compojure "1.1.6"]
                 [hiccup "1.0.5"]
                 [ring-server "0.3.1"]
                 [org.clojure/java.jdbc "0.3.3"]
                 [postgresql/postgresql "9.3-1101.jdbc41"]
                 [clj-pdf "1.11.6"]]
  :plugins [[lein-ring "0.8.10"]]
  :ring {:handler tonglindb.handler/app
         :init tonglindb.handler/init
         :destroy tonglindb.handler/destroy}
  :aot :all
  :profiles
  {:production
   {:ring
    {:open-browser? false, :stacktraces? false, :auto-reload? false}}
   :dev
   {:dependencies [[ring-mock "0.1.5"] [ring/ring-devel "1.2.1"]]}})

and the db.clj which caused the compiling errors:

(ns tonglindb.models.db
  (:require [clojure.java.jdbc :as sql]))

(def db {:subprotocol "postgresql"
         :subname "//localhost/tonglindb"
         :user "tonglin"
         :password "12345"})

(defn create-employee-table []
  (sql/create-table
    :employee
    [:name "varchar(50)"]
    [:occupation "varchar(50)"]
    [:place "varchar(50)"]
    [:country "varchar(50)"]))

(sql/with-connection
  db
  (create-employee-table)
  (sql/insert-rows
    :employee
    ["Albert Einstein", "Engineer", "Ulm", "Germany"]
    ["Alfred Hitchcock", "Movie Director", "London", "UK"]
    ["Wernher von Braun", "Rocket Scientist", "Wyrzysk", "Poland"]
    ["Sigmund Freud", "Neurologist", "Pribor", "Czech Public"]
    ["Michael Schumacher", "F1 Racer", "Cologne", "Germany"]))

(defn read-employees []
  (sql/with-connection db
    (sql/with-query-results rs ["select * from employee"] (doall rs))))

The version of postgresql is 9.3.3. Could someone tell me how can I solve this problem? Thanks a lot!

Was it helpful?

Solution

The 0.3.x versions of org.clojure/java.jdbc have made some changes to the functionality contained in the clojure.java.jdbc namespace, including the removal of create-table (amongst others).

So, either you use an earlier version of the library or you access the deprecated namespace that ships with the current one. I.e., replace

(ns tonglindb.models.db
  (:require [clojure.java.jdbc :as sql]))

with

(ns tonglindb.models.db
  (:require [clojure.java.jdbc.deprecated :as sql]))

Doc: http://clojure.github.io/java.jdbc/#clojure.java.jdbc.deprecated

This namespace contains the old API (0.2.3) which was deprecated in the 0.3.0
release and is provided for backward compatibility. This API will be removed
completely before a 1.0.0 release so will need to migrate code to the new API
before that release.

Edit: As Daniel Neal mentioned in the comments, the DDL functions moved to seancorfield/jsql. It's probably better to rely on that library instead of a deprecated namespace.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top