"使用Clojure进行Web开发" - 编译错误:没有这样的var:sql / create-table

时间:2014-03-19 05:06:15

标签: jdbc clojure compojure

我正在使用Dmitri Sotnikov的“使用Clojure进行Web开发”来学习如何构建Web应用程序。当我尝试运行一些代码来访问数据库时,会发生一些编译错误:

运行后

> lein ring server

我得到了:

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

(在此之前我运行了 lein deps ,它什么也没有返回。我认为这意味着没有依赖性问题。)

这是我的 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"]]}})

db.clj 导致编译错误:

(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))))

postgresql的版本是9.3.3。有人能告诉我怎样才能解决这个问题?非常感谢!

1 个答案:

答案 0 :(得分:6)

0.3.x版本org.clojure/java.jdbc已对clojure.java.jdbc命名空间中包含的功能进行了一些更改,包括删除create-table(以及其他内容)。

因此,您要么使用该库的早期版本,要么访问当前版本附带的deprecated命名空间。即,替换

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

(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.

编辑:正如Daniel Neal在评论中提到的,DDL功能已移至seancorfield/jsql。依赖该库而不是弃用的命名空间可能更好。