honeysql

Discussion of https://github.com/seancorfield/honeysql :slightly_smiling_face:
Scott Starkey 2021-03-04T21:15:29.082600Z

Hi all - I have set up a postgresql server with my web host provider (Bluehost) and I’m trying to figure out the steps to basic database access. Is there a “Hello world!” guide to how to get set up with HoneySQL (or any other SQL)? I’ve set up a database and a user, but I’m a little lost on how to connect to it.

seancorfield 2021-03-04T21:23:07.083400Z

HoneySQL just turns Clojure data into SQL. It doesn't have anything to do with actually connecting to a database.

seancorfield 2021-03-04T21:23:39.083900Z

You need to use next.jdbc to actually run any SQL against a database.

seancorfield 2021-03-04T21:24:44.085200Z

You would generally define a hash map that describes your database setup: {:dbtype "postgres" :dbname "whatever" :user "dbauser" :password "secret" :host "ip-or-hostname"}

seancorfield 2021-03-04T21:25:35.086200Z

Then you can run SQL against the DB via (next.jdbc/execute! db-spec (sql/format {:your :honeysql :data :here}))

Scott Starkey 2021-03-08T21:19:16.098100Z

Hi Sean and others. I’m starting to take a hack at this, and I appreciate any help you could provide. I’ve done the basics at the link above, and got the following error message:

db-hello-world.core> (def ds (jdbc/get-datasource testmap))
;; => #'db-hello-world.core/ds
db-hello-world.core> (jdbc/execute! ds ["
create table address (
  id int auto_increment primary key,
  name varchar(32),
  email varchar(255)
)"])

Execution error (SQLException) at java.sql.DriverManager/getConnection (DriverManager.java:689).
No suitable driver found for ***My IP Address***:***My port number***/***My db  name***
I’m not sure whether this is an error with my host provider giving me the wrong IP/port info, or if this is a problem with something else. “No suitable driver” makes it sound like I’m missing a driver! Can you help?

seancorfield 2021-03-08T21:22:47.098300Z

What is your testmap?

seancorfield 2021-03-08T21:23:27.098500Z

That error generally means you have not specified a valid :dbtype

seancorfield 2021-03-08T21:26:23.098700Z

@scotto You're using PostgreSQL? Hosted somewhere non-local?

Scott Starkey 2021-03-08T21:27:48.098900Z

Yeah, I want to host it at my hosting provider (bluehost). Eventually I would like it to be a web app.

seancorfield 2021-03-08T21:29:27.099100Z

So I would expect something like this in your testmap: {:dbtype "postgres" :dbname "..." :user "..." :password "..." :host "..."} (assuming a default port for PostgreSQL which is 5432)

Scott Starkey 2021-03-08T21:29:56.099300Z

My testmap is

{:dbtype "postgres",
 :dbname "", ; redacted
 :user "", ; redacted
 :password "", ; redacted 
 :host "162.241.216.221", ; The ip number that Bluehost told me.
 :port 3306} ; The port number that Bluehost told me.

seancorfield 2021-03-08T21:30:17.099500Z

3306 is normally MySQL, not PostgreSQL.

Scott Starkey 2021-03-08T21:30:25.099700Z

Ahhh…

Scott Starkey 2021-03-08T21:30:35.099900Z

They might have given me wrong info.

seancorfield 2021-03-08T21:30:43.100100Z

In your project setup, do you have the appropriate database driver added as a dependency?

seancorfield 2021-03-08T21:31:11.100300Z

Per the Getting Started docs: "In addition, you will need to add dependencies for the JDBC drivers you wish to use for whatever databases you are using."

Scott Starkey 2021-03-08T21:31:37.100500Z

:dependencies [[org.clojure/clojure "1.10.3"]
                 [seancorfield/next.jdbc "1.1.613"]]
I think so… ^

seancorfield 2021-03-08T21:31:51.100700Z

No, you have no JDBC drivers there.

seancorfield 2021-03-08T21:32:33.100900Z

The docs link to this part of the next.jdbc project https://github.com/seancorfield/next-jdbc/blob/develop/deps.edn#L10-L27 which shows a list of JDBC driver dependencies.

Scott Starkey 2021-03-08T21:32:36.101200Z

Oh… sorry. Newbie…

seancorfield 2021-03-08T21:33:25.101400Z

As you said “No suitable driver” makes it sound like I’m missing a driver! Can you help? -- so you were correct that you are missing a driver.

Scott Starkey 2021-03-08T21:33:33.101600Z

When this is fixed, I will buy you the beverage of your choice. 🙂 :thumbsup:

seancorfield 2021-03-08T21:33:48.101800Z

PostgreSQL: org.postgresql/postgresql {:mvn/version "42.2.10"}

seancorfield 2021-03-08T21:33:59.102Z

MySQL: mysql/mysql-connector-java {:mvn/version "8.0.19"}

seancorfield 2021-03-08T21:34:26.102200Z

Those aren't quite the latest versions but they should be recent enough for you.

seancorfield 2021-03-08T21:35:27.102400Z

If Bluehost is really offering you MySQL instead of PostgreSQL, you want :dbtype "mysql" instead of :dbtype "postgres" (and you can omit :port 3306 since that is the default)

Scott Starkey 2021-03-08T21:35:32.102600Z

I’m using Leiningen, so I assume I need: [org.postgresql/postgresql "42.2.18.jre7"]

seancorfield 2021-03-08T21:36:10.102800Z

Are you using Java 7 or a more recent JVM?

Scott Starkey 2021-03-08T21:36:59.103Z

java version "1.8.0_201"

seancorfield 2021-03-08T21:37:06.103200Z

Looks like this is the most recent for Java 8 and above: https://search.maven.org/artifact/org.postgresql/postgresql/42.2.19/jar

seancorfield 2021-03-08T21:37:32.103500Z

Scroll down and you'll see the Leiningen dep on the right hand side: [org.postgresql/postgresql "42.2.19"]

Scott Starkey 2021-03-08T21:37:49.103700Z

Again. Thank you so much!

seancorfield 2021-03-08T21:37:50.103900Z

The .jre7 and .jre6 versions are for older JVMs.

seancorfield 2021-03-08T21:40:21.104100Z

If you actually are connecting to MySQL, this is the latest version I recommend: https://search.maven.org/artifact/mysql/mysql-connector-java/8.0.22/jar (there's an 8.0.23 but it has a number of changes that can be a bit problematic, depending on your setup so I'd say stick to 8.0.22 for now).

Scott Starkey 2021-03-04T21:34:18.086700Z

Thank you, kindly!