blob: f098c9aad0d38a55b7d58175f93ef067c81e9d90 (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
|
(ns clojure.contrib.sql.test
(:use clojure.contrib.sql))
(Class/forName "org.apache.derby.jdbc.EmbeddedDriver")
(defn db []
(get-connection "derby" "/tmp/clojure.contrib.sql.test.db;create=true"))
(defn db-drop []
(with-connection con (db)
(try
(execute-commands con
["drop table fruit"])
(catch Exception e))))
(defn db-write []
(db-drop)
(with-connection con (db)
(execute-commands con
["create table fruit (name varchar(32), appearance varchar(32), cost int, grade real)"])
(seq
(execute-prepared-statement con
"insert into fruit values (?, ?, ?, ?)"
[["Apple" "red" 59 87]
["Banana" "yellow" 29 92.2]
["Peach" "fuzzy" 139 90.0]
["Orange" "juicy" 89 88.6]]))))
(defn db-read []
(with-connection con (db)
(with-query-results rec con
"select * from fruit"
(println rec))))
(defn db-grade-a []
(with-connection con (db)
(with-query-results rec con
"select name, cost from fruit where grade >= 90"
(println rec))))
(defn db-exception []
(with-connection con (db)
(execute-prepared-statement con
"insert into fruit (name, appearance) values (?, ?)"
[["Grape" "yummy"]
["Pear" "bruised"]])
(throw (Exception. "an exception"))))
|