Fork me on GitHub
#sql
<
2018-01-18
>
Jacob00:01:02

Hey guys Im new to clojure and I have a question? I am trying to convert all of my shell and sql scripts to clojure so that I can test and whats going on with my scripts. I can select data just fine. I can insert data into the database just fine manually but I would like to be able to select from my database then manipulate the data from my select statement then insert the data into another table. Here is my select query that I am using

Jacob00:01:19

(jdbc/query db ["select id, SUM(amount) from product where dt >= '2017-12-1' and dt <= '2017-12-31'"])

Jacob00:01:35

(jdbc/query db ["insert into new_products (query) values (?);" "query"]))

seancorfield01:01:17

First off, query is for selects only. You can use insert-multi! which accepts a sequence of rows -- which is what query returns:

(let [data (jdbc/query db ["select id, sum(amount) from product ..."])]
  (jdbc/insert-multi! db :new_products data))

seancorfield01:01:06

You could also use execute! with a single SQL state that did both the select and the insert.

Jacob03:01:43

ok that looks like it will work thank you @seancorfield ill try it out

Jacob06:01:24

i keep getting this error

Jacob06:01:15

I was able to get the let to work with jdbc it worked greate i couldnt get the execute to work though

seancorfield06:01:08

What code is around balances/core.clj line 20 @jisaacs46?

seancorfield06:01:22

The error means you have a string in a place where Clojure is expecting a function.

seancorfield07:01:06

No, you have an extra set of parentheses around the expression.

seancorfield07:01:24

The create-table-ddl function returns a string and then you're trying to call that string - because you have it wrapped in parentheses.

Jacob07:01:47

Lol oh man thank you so much lol 😂 I’ll give that a try in the morning @seancorfield

Jacob07:01:10

I missed that extra set of parentheses

seancorfield07:01:29

(and I see you have a query with a drop command -- query only works for select, you want db-do-commands for DDL per the documentation)

Jacob07:01:01

Alright I’ll give that a try thanks again you’ve been a big help.

Jacob16:01:50

Alright I’ll give that a try thanks again you’ve been a big help.

Jacob16:01:04

That worked thank you. @seancorfield I have another question? Do I need to close the database connections and if I do do I just do (db connectio.close)

seancorfield16:01:15

As long as you don't directly call jdbc/get-connection, the connections will all be managed for you. Each operation opens and closes a connection.

seancorfield16:01:06

That's why connection pooling is a good idea (for performance) since opening and closing a full database connection can be expensive, but once you have a connection pool, "opening" (getting) and "closing" (returning) a connection against that pool is cheap.

seancorfield23:01:56

Your call is incorrect -- it should be (jdbc/db-do-commands db ["DROP TABLE fruit" "CREATE TABLE fruit..."]) -- it takes a collection (sequence) of commands. You specified them as separate arguments.