-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathprintAuthAndCountry.hs
39 lines (34 loc) · 1.21 KB
/
printAuthAndCountry.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
37
38
39
-- import database libraries
import Database.HDBC.PostgreSQL
import Database.HDBC
{-
Define a function that will fetch all rows from the table authority,
get the connected countries, and print them to the screen in a friendly format.
-}
printAuthAndCountry = do
-- Connect to the Database
conn <- connectPostgreSQL "dbname=smf"
-- to test the connection
t <- getTables conn
putStrLn (if t == [] then "no table found" else head t)
-- or? if t == [] then putStrLn "no table found"
-- else mapM putStrLn 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