forked from CivicLabsBelgium/ShowMeFinance
-
Notifications
You must be signed in to change notification settings - Fork 0
/
readCountry.hs
36 lines (31 loc) · 1.1 KB
/
readCountry.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
36
-- import database libraries
import Database.HDBC.PostgreSQL
import Database.HDBC
{-
Define a function that will fetch all rows from the table country,
and print them to the screen in a friendly format.
-}
readCountry = do -- Connect to the Database
conn <- connectPostgreSQL "dbname=smf"
-- test to see if the connection is ok
-- t <- getTables conn
-- putStrLn (if t == [] then "no table found" else head t)
-- run the query and store the results in r
r <- quickQuery' conn
"SELECT iso, countryname from country order by iso" []
-- convert each row into a string
let stringRows = map convRow r
-- print a title
putStrLn "Show Me Finance --- Countries"
putStrLn "ISO Country Name"
putStrLn " "
-- print the rows output
mapM_ putStrLn stringRows
-- and disconnect from the database
disconnect conn
where convRow :: [SqlValue] -> String
convRow [sqlISO, sqlCountryName] =
show iso ++ ": " ++ name
where iso = (fromSql sqlISO)::String
name = (fromSql sqlCountryName)::String
convRow x = fail $ "Unexpected result: " ++ show x