[Haskell-cafe] Re: simple hsql question
explicitcall at googlemail.com
Mon Aug 17 18:07:51 EDT 2009
Alexander Kotelnikov <sacha at myxomop.com> writes:
> 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.
More information about the Haskell-Cafe