gpt4 book ai didi

postgresql - 查询 Postgres 数据库 - Clojure

转载 作者:行者123 更新时间:2023-11-29 12:56:24 25 4
gpt4 key购买 nike

我正在尝试查询在我的机器上本地设置的 postgres 数据库。

我创建了一个文件

(ns website.db
(:require [clojure.java.jdbc :as jdbc]))

(def database
{:classname "com.postgres.jdbc.Driver"
:subprotocol "postgres"
:subname "mydb" ; In the guide this was //127.0.0.1:3306/mydb. Is the first part my computer IP address?
:user "admin"
:password "secret"})

如果我然后尝试使用(在 REPL 中)查询数据库

(jdbc/query database ["SELECT * FROM table"])

我收到错误 ConnectException Connection refused java.net.PlainSocketImpl.socketConnect (PlainSocketImpl.java:-2)

请注意,我希望数据库为空,因为我还没有向数据库中输入任何信息

我在定义数据库时犯了什么错误?

插入用户是否正确:

(defn register-user! [db name]
(jdbc/insert! db :user {:name name}))

最佳答案

clojure.java.jdbc 文档站点没有涵盖所有可能性,也没有太多细节。

我会指出您 the doc of get-connection功能相当广泛:

(...) db-spec is usually a map containing connection
parameters but can also be a URI or a String. The various possibilities are described
below:
DriverManager (preferred):
:dbtype (required) a String, the type of the database (the jdbc subprotocol)
:dbname (required) a String, the name of the database
:host (optional) a String, the host name/IP of the database
(defaults to 127.0.0.1)
:port (optional) a Long, the port of the database
(defaults to 3306 for mysql, 1433 for mssql/jtds, else nil)
(others) (optional) passed to the driver as properties.
Raw:
:connection-uri (required) a String
Passed directly to DriverManager/getConnection
Other formats accepted:
Existing Connection:
:connection (required) an existing open connection that can be used
but cannot be closed (only the parent connection can be closed)
DriverManager (alternative / legacy style):
:subprotocol (required) a String, the jdbc subprotocol
:subname (required) a String, the jdbc subname
:classname (optional) a String, the jdbc driver class name
(others) (optional) passed to the driver as properties.
Factory:
:factory (required) a function of one argument, a map of params
(others) (optional) passed to the factory function in a map
DataSource:
:datasource (required) a javax.sql.DataSource
:username (optional) a String
:user (optional) a String - an alternate alias for :username
(added after 0.3.0-beta2 for consistency JDBC-74)
:password (optional) a String, required if :username is supplied
JNDI:
:name (required) a String or javax.naming.Name
:environment (optional) a java.util.Map
java.net.URI:
Parsed JDBC connection string (see java.lang.String format next)
java.lang.String:
subprotocol://user:password@host:post/subname
An optional prefix of jdbc: is allowed."

最新的clojure.java.jdbc 版本还包括Clojure specs for db-spec map .

您的情况可能是:

{:dbtype "postgresql")
:dbname "mydb"
:host "127.0.0.1"
:port 5432
:user "admin"
:password "secret"}

关于postgresql - 查询 Postgres 数据库 - Clojure,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42316970/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com