-
Notifications
You must be signed in to change notification settings - Fork 2
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Running manual insert or update queries? #1
Comments
Actually, is there a way to just get one of the connections so I can run the newer clojure.java.jdbc functions (that require you to pass the connection in)? |
java.jdbc changed a lot since the last time I read it. Some time is needed to figure out what's going on. As for how to get the connection: (:ds @db-factory) ; javax.sql.DataSource, a Connection pool
(.getConnection ^DataSource (:ds @db-factory)) ; java.sql.Connection |
Thanks. I haven't used atoms before - will dereferencing it work in my own code, or will I have to place that in the dbcp.clj source? |
(:use [org.httpkit.dbcp :only [db-factory]]) should do the trick |
Thanks again! I just tried both out in the leiningen repl (not sure if that affects it) with the following code ( (jdbc/query (db-conn) "SELECT * FROM posts LIMIT 1") but I get the same error for both (although the .getConnection one takes a bit of time to actually get the connection first):
I had a look at the jdbc source and it seems like it wasn't picking it up as a datasource (it expects a map). I tried both wrapping it like
Any ideas? EDIT: I spotted this on the JDBC documentation - it may be helpful: http://clojure.github.io/java.jdbc/doc/clojure/java/jdbc/ConnectionPooling.html |
From the source code: (defn db-find-connection
"Returns the current database connection (or nil if there is none)"
^java.sql.Connection [db]
(and (map? db)
(:connection db))) ;; db-conn
{:connection (.getConnection ^DataSource (:ds @db-factory))} |
or from the (jdbc/query @db-factory "SELECT * FROM posts LIMIT 1")
|
Both of those still get the null pointer exception that I posted earlier. I think it may be the postgres driver being odd. I might try an earlier version |
I just tried an earlier postgres driver and I get the same null pointer exception (but a different line of code) |
have you call the use-database! ?
|
Yeah I have. Sorry about that - i forgot to mention it |
I just realised I am an idiot. I should have read the documentation more thoroughly. (jdbc/query @db-factory ["SELECT * FROM posts LIMIT 1"]) worked properly. I forgot to put the query in a vector. |
I'm using this in one of my projects at work, and one of the features I'm implementing needs some stuff that doesn't seem possible with the
insert-record
orupdate-values
functions. I'm trying to increment a counter in a row. If I was doing it in SQL I would do something along the lines of this:I had noticed the
with-db
macro, but when looking at the official clojure jdbc library, they say thatwith-connection
is deprecated and shouldn't be used. Is there a way to just run a query like this?Also, how would I go about doing a database transaction?
Thanks for any help!
Tom
The text was updated successfully, but these errors were encountered: