-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhdbc-test.hs
36 lines (27 loc) · 998 Bytes
/
hdbc-test.hs
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
module Main where
import Database.HDBC.PostgreSQL (connectPostgreSQL)
-- import Database.HDBC.Sqlite3 (connectSqlite3)
import Database.HDBC
main = query 1000
sql_to_string :: SqlValue -> String
sql_to_string sql_value = case fromSql sql_value of
Just x -> x
Nothing -> "NULL"
query :: Int -> IO ()
query limit =
do
-- conn <- connectSqlite3 "sqlite3.db"
conn <- connectPostgreSQL "user=svk password=svk host=localhost port=5432 dbname=haskell"
-- Run the query and store the results in r
r <- quickQuery' conn
"SELECT * FROM \"Quote\" ORDER BY datetime DESC LIMIT ?"
[toSql limit]
-- Convert each row into a String
let stringRows = map convRow r
-- Print the rows out
mapM_ putStrLn stringRows
-- And disconnect from the database
disconnect conn
where convRow :: [SqlValue] -> String
convRow sql_values =
foldl1 (++) (map sql_to_string sql_values)