Dimas Angga Saputra
Dimas Angga Saputra

Reputation: 221

Clojure PostgreSQL JDBC keep getting error executing query

i hope someone could help me with this,

So i'm building database backed website with clojure and postgresql, but i keep getting error. Here are the code and error on REPL:

(ns app.config-postgre
  (require [clojure.java.jdbc :as sql]))

(def db1
  {:classname "org.postgresql.Driver"
   :subprotocol "postgresql"
   :subname "//localhost:5432/dbname"
   :username "username"
   :password "password}) 
;;that's not the real username and password, the real one is right

here goes the code that caused error :

(sql/insert! db1 :user123
             {:username "user" :password "pass" :user-id "1"})
;;PSQLException ERROR: syntax error at or near "user"
  Position: 43  org.postgresql.core.v3.QueryExecutorImpl.receiveErrorResponse (QueryExecutorImpl.java:2157)

and

(sql/execute! db1 ["INSERT INTO user123 VALUES ('{\"user\", \"pass\" ,1}')"])
;;PSQLException ERROR: null value in column "password" violates not-null constraint
  Detail: Failing row contains ({user,pass,1}, null, null).  org.postgresql.core.v3.QueryExecutorImpl.receiveErrorResponse (QueryExecutorImpl.java:2157)

and

(sql/execute! db1 ["INSERT INTO user123 VALUES ('{\"user\"},{\"pass\"},{1}')"])
;;PSQLException ERROR: malformed array literal: "{"user"},{"pass"},{1}"
  Detail: Junk after closing right brace.
  Position: 29  org.postgresql.core.v3.QueryExecutorImpl.receiveErrorResponse (QueryExecutorImpl.java:2157)

and

(sql/execute! db1 ["INSERT INTO user123 VALUES ('\"user\",\"pass\",1')"])
;;PSQLException ERROR: malformed array literal: ""user","pass",1"
  Detail: Array value must start with "{" or dimension information.
  Position: 29  org.postgresql.core.v3.QueryExecutorImpl.receiveErrorResponse (QueryExecutorImpl.java:2157)

and

(sql/execute! db1 ["INSERT INTO user123 VALUES ('user','pass',1)"])
;;PSQLException ERROR: malformed array literal: "user"
  Detail: Array value must start with "{" or dimension information.
  Position: 29  org.postgresql.core.v3.QueryExecutorImpl.receiveErrorResponse (QueryExecutorImpl.java:2157)

Any suggestion? Thank you in advance

edit schema for table user123 :

CREATE TABLE user123
(
  username character varying(100)[] NOT NULL,
  password character varying(100)[] NOT NULL,
  "user-id" integer NOT NULL,
  CONSTRAINT user_pkey PRIMARY KEY ("user-id")
)

Upvotes: 0

Views: 1323

Answers (3)

naod
naod

Reputation: 1

I think your should change :username to :user in your db specification.

Upvotes: 0

WIZARDELF
WIZARDELF

Reputation: 3895

(sql/execute! db1 ["INSERT INTO user123 VALUES ({'user'}, {'pass'}, 1);"])

Upvotes: 0

Sergii Lagutin
Sergii Lagutin

Reputation: 10681

Use (sql/execute! db1 ["INSERT INTO user123 VALUES (?, ?, ?)" "user" "pass" 1]) for your purpose

Upvotes: 1

Related Questions