簡體   English   中英

在 Clojure 中需要 next.jdbc 時出現錯誤“沒有這樣的命名空間:jdbc”

[英]Error "No such namespace: jdbc" when requiring next.jdbc in Clojure

我在嘗試創建 postgres 數據庫時遇到了 require 和/或 next.jdbc 錯誤:

Unhandled java.io.FileNotFoundException
   Could not locate next/jdbc__init.class, next/jdbc.clj or next/jdbc.cljc on
   classpath. 

以及以下。 每當我嘗試在 cljblog.db 命名空間中調用 jdbc 函數時,它就會出現。

1. Caused by java.lang.RuntimeException
   No such namespace: jdbc
   

這是我的數據庫設置:

(ns cljblog.db)
(require '[next.jdbc :as jdbc])

(def db
  {:dbtype "postgresql"
   :dbname "cljblog"
   :host "localhost"
   :user "postgres"
   :password "postgres"})

(def ds (jdbc/get-datasource db))

(def ds (jdbc/get-datasource db))
(def conn (jdbc/get-connection ds))

(jdbc/execute! conn ["
-- postgresql version
drop table if exists posts?;
create table posts (
  id int,
  title varchar(255),
  body text,
author varchar(25)
"])

(jdbc/execute! conn ["
insert into posts(title,body,author)
  values('Working with postgres',
'this is my first attempt at working
with postgres in Clojure', 'Sam Dees')"])

(def result-set
  (jdbc/execute!
   conn
   ["select * from posts"]))

和 project.clj

(defproject cljblog "0.1.0-SNAPSHOT"
  :description "FIXME: write description"
  :url "http://example.com/FIXME"
  :min-lein-version "2.0.0"
  :dependencies [[org.clojure/clojure "1.10.0"]
                 [compojure "1.6.1"]
                 [ring/ring-defaults "0.3.2"]
                 [hiccup "1.0.5"]
                 [com.github.seancorfield/next.jdbc "1.2.780"]
                 [org.postgresql/postgresql "9.4-1201-jdbc41"]]
  :plugins [[lein-ring "0.12.5"]]
  :ring {:handler cljblog.handler/app}
  :repl-options {:init-ns clj-jdbc.core}
  :profiles
  {:dev {:dependencies [[javax.servlet/servlet-api "2.5"]
                        [ring/ring-mock "0.3.2"]]}})

*為簡潔起見,我截斷了完整的錯誤消息。 *postgres 與系統上運行的活動服務器一起安裝

假設您在添加這些依賴項后重新啟動了 REPL,您的ns表單應如下所示:

(ns cljblog.db
  (:require [next.jdbc :as jdbc]))

此外,您還有:

  :repl-options {:init-ns clj-jdbc.core}

但是您沒有向我們展示我懷疑確實發生錯誤的名稱空間(在文件src/clj_jdbc/core.clj )。

您可能希望使用 Docker 容器中的 Postgres 以及next.jdbc來克隆此演示項目以進行訪問:

https://github.com/cloojure/demo-jdbc-next

按照自述文件中的說明進行操作,它應該可以立即工作。 還有將next.jdbc與 H2 數據庫一起使用的示例。

當您運行測試時,您將看到一切正常:

> lein clean; lein test 

lein test _bootstrap

-------------------------------
   Clojure 1.10.3    Java 17
-------------------------------

lein test tst.demo.jdbc-h2

lein test tst.demo.jdbc-postgres

Ran 7 tests containing 42 assertions.
0 failures, 0 errors.

所有操作都在測試命名空間中。 文件test/tst/demo/jdbc_postgres.clj開始如下:

(ns tst.demo.jdbc-postgres
  (:use demo.core tupelo.core tupelo.test)
  (:require
    [next.jdbc :as jdbc]
    [next.jdbc.result-set :as rs]
    [next.jdbc.sql :as sql]
    ))

; ***** change this value to `false` to disable PostgreSQL unit tests *****
(def postgres-enable true)

;---------------------------------------------------------------------------------------------------
(when postgres-enable

  (def db-info {:dbtype   "postgres"
                :dbname   "example"
                :user     "postgres"
                :password "docker"
                })

  (def ds (jdbc/get-datasource db-info))

  ; NOTE: ***** Must MANUALLY  create DB 'example' before run this test! *****
  ;       In PSQL, do `create database example;`.  See the README.
  (dotest
    (jdbc/execute! ds ["drop table if exists address"])
    (let [r11 (jdbc/execute! ds ["
                create table address (
                  id      serial primary key,
                  name    varchar(32),
                  email   varchar(255)
                ) "])
          r12 (jdbc/execute! ds ["
                insert into address(name, email)
                  values( 'Homer Simpson', 'homer@springfield.co' ) "])
          r13 (jdbc/execute! ds ["select * from address "])
          ]
      (is= r11 [#:next.jdbc{:update-count 0}])
      (is= r12 [#:next.jdbc{:update-count 1}])
      (is= r13 [#:address{:id 1, :name "Homer Simpson", :email "homer@springfield.co"}]))

<snip>

我注意到您的project.clj似乎有問題。 我的看起來像:

  :dependencies [
                 [com.h2database/h2 "1.4.200"] ; #todo cannot upgrade yet or crash!
                 [hikari-cp "2.14.0"]
                 [org.clojure/clojure "1.11.1"]
                 [org.clojure/test.check "1.1.1"]
                 [org.postgresql/postgresql "42.3.5"]
                 [prismatic/schema "1.2.1"]
                 [com.github.seancorfield/next.jdbc "1.2.780"] 
                 [tupelo "22.05.20"]
                 ]

所以對org.postgresql/postgresql的依賴可能是問題的一部分。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM