简体   繁体   English

在 Clojure 中需要 next.jdbc 时出现错误“没有这样的命名空间:jdbc”

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

I'm running into require and or next.jdbc errors when I attempt to create a postgres database:我在尝试创建 postgres 数据库时遇到了 require 和/或 next.jdbc 错误:

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

and the following.以及以下。 Which appears anytime I attempt to call jdbc functions within the cljblog.db namespace.每当我尝试在 cljblog.db 命名空间中调用 jdbc 函数时,它就会出现。

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

Here's my db setup:这是我的数据库设置:

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

and the project.clj和 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"]]}})

*I've truncated the full error messages for brevity. *为简洁起见,我截断了完整的错误消息。 *postgres is installed with an active server running on the system *postgres 与系统上运行的活动服务器一起安装

Assuming you restarted your REPL after adding those dependencies, your ns form should look like this:假设您在添加这些依赖项后重新启动了 REPL,您的ns表单应如下所示:

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

In addition, you have:此外,您还有:

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

but you didn't show us that namespace (in the file src/clj_jdbc/core.clj ) which I suspect is where the error is really occurring.但是您没有向我们展示我怀疑确实发生错误的名称空间(在文件src/clj_jdbc/core.clj )。

You may wish to clone this demo project using Postgres in a Docker container along with next.jdbc for access:您可能希望使用 Docker 容器中的 Postgres 以及next.jdbc来克隆此演示项目以进行访问:

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

Follow along the instructions from the README and it should work right away.按照自述文件中的说明进行操作,它应该可以立即工作。 There are also examples of using next.jdbc with the H2 database.还有将next.jdbc与 H2 数据库一起使用的示例。

When you run the tests, you will see everything working:当您运行测试时,您将看到一切正常:

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

All of the action is in the test namespaces.所有操作都在测试命名空间中。 The file test/tst/demo/jdbc_postgres.clj starts out like so:文件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>

I've noticed that you seem to have something wrong in your project.clj .我注意到您的project.clj似乎有问题。 Mine looks like:我的看起来像:

  :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"]
                 ]

so the dependency on org.postgresql/postgresql may be part of the problem.所以对org.postgresql/postgresql的依赖可能是问题的一部分。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM