simple hsql question

Hi I wanted to see what access to databases HSQL provides and I stumbled in the very beginning. Assume I have a table map1 with attributes "i" and "s" interger and varchar() respectively. The following code fails (with segfault) for me. And I see no other way to tell compiler that I am expecting an interger to be found as 'i' in a fetched row. import Database.HSQL import Database.HSQL.MySQL main :: IO () main = do c <- connect "localhost" "tx_test" "sacha" "" s <- query c "SELECT i FROM map1" print $ getFieldsTypes s i <- (getFieldValue s "i")::IO Int print i disconnect c -- Alexander Kotelnikov Saint-Petersburg, Russia

Ok, let me ask it in another way. Is there a good way to access databases, mysql in particular, from haskell program?
On Sun, 16 Aug 2009 18:54:32 +0400 "AK" == Alexander Kotelnikov
wrote: AK> AK> Hi AK> I wanted to see what access to databases HSQL provides and I stumbled in AK> the very beginning. Assume I have a table map1 with attributes "i" and "s" AK> interger and varchar() respectively. The following code fails (with AK> segfault) for me. And I see no other way to tell compiler that I am AK> expecting an interger to be found as 'i' in a fetched row. AK> AK> import Database.HSQL AK> import Database.HSQL.MySQL AK> AK> main :: IO () AK> main = do AK> c <- connect "localhost" "tx_test" "sacha" "" AK> s <- query c "SELECT i FROM map1" AK> print $ getFieldsTypes s AK> i <- (getFieldValue s "i")::IO Int AK> print i AK> disconnect c AK> AK> -- AK> Alexander Kotelnikov AK> Saint-Petersburg, Russia AK> -- Alexander Kotelnikov Saint-Petersburg, Russia

Alexander Kotelnikov
Ok, let me ask it in another way. Is there a good way to access databases, mysql in particular, from haskell program?
Use HDBC or Takusen. You can find them on hackage. HDBC is fairly usable, but you must write SQL queries by yourself or use some simple machinery to construct queries. I've written a bunch of helper functions to construct queries as strings, e.g.:
insert :: String -> [String] -> String -> String insert t = (++) . (" INSERT " ++) . (" INTO " ++) . (t ++) . (" (" ++) . (++ ") ") . join ", "
select :: [String] -> String -> String select = (++) . (" SELECT " ++) . join ", "
from :: [String] -> String -> String from = (++) . (" FROM " ++) . join ", "
In the end your query looks like:
query' (select ["max(cheque)"] . from ["history"] $ ";"
In this way you avoid typical errors which emerge when you write simple SQL query strings. Though you don't get true static typing. There are haskelldb and Takusen, which provide more elaborate way of connecting to RDBMS, they demand more investigation though.
participants (2)
-
Alexander Kotelnikov
-
Max Desyatov