In this lab, we will create a simple application to use SQL database. This lab uses Apache Derby http://db.apache.org/derby/ SQL database since Derby is a zero setup database. We can skip whole lot of database installation and setting.
The first step is to create a Clojure project.
Add three libraries,
in the dependencies of your
project.clj. The file will look like below:
The second step is to write an application code.
In this lab, we will add code to
The file is generated by leiningen and has a sample
You may delete unused
Our SQL database application code is below. You may copy & paste entire code in your
The next step is to use our application code on the repl.
Assuming you are in the directory
hello-derby, which contains
start repl by
The first time you run
lein repl in this directory, leiningen downloads
jar archives necessaery to run this project.
When the repl gets ready, load the application on your repl by
Then, test actually
hello-derby.core is successfully loaded by
checking the Vars,
If you get an exception when you type
probably, your code has a typo, unmatched parentheses, or some errors.
Correct the error on the editor if you have.
You can reload the editied code with
If everything looks ok, create
posts table and write some posts.
Then, see those are in database.
Could you see the result of
We added documents in our functions. You can see those by
So far, we have only one query function to get all. However, database system as well as SQL query is there to select data from all.
The next work is to add two query functions in our application.
find-post functions finds a record by id.
order-by-time function finds one of ids, titles, or bodies ordered by the time created.
Back on the repl, reload your code and make queries using newly added functions.
If you can see the results of the queries, add more posts using
write-post function and
try queries again.
For further coding, these are helpful references.