[Haskell-cafe] HDBC convert [SqlValue] without muchos boilerplate

Iain Barnett iainspeed at gmail.com
Thu Feb 11 04:55:20 EST 2010


Hi,

I'm trying to get to grips with HDBC and have the following problem. When I run a query that returns a result set, each row comes back as a [SqlValue]. Naively, I thought the following function would convert a [SqlValue] into a string, but instead I get the error below. 

convrow2 :: [SqlValue] -> String	
convrow2 (x:xs) = foldl (\i j -> i ++ " | " ++ show j ) (show (fromSql x)) xs


Prelude> :l TasksSimple.lhs 
[1 of 1] Compiling Main             ( TasksSimple.lhs, interpreted )

TasksSimple.lhs:126:65:
    No instance for (convertible-1.0.5:Data.Convertible.Base.Convertible
                       SqlValue a)
      arising from a use of `fromSql' at TasksSimple.lhs:126:65-73
    Possible fix:
      add an instance declaration for
      (convertible-1.0.5:Data.Convertible.Base.Convertible SqlValue a)
    In the first argument of `show', namely `(fromSql x)'
    In the second argument of `foldl', namely `(show (fromSql x))'
    In the expression:
        foldl (\ i j -> i ++ " | " ++ show j) (show (fromSql x)) xs
Failed, modules loaded: none.


I tried looking at how to add an instance declaration for convertible, but was stumped.


This code, however, works in GHCi. Would anyone know how to convert from [SqlValue] in a straightforward way without having to specify every field "by hand" ? I don't fancy doing this for each sql statement I need to run.

convrow1 :: [SqlValue] -> String	
convrow1 [tasksid,title,added] = 
							show ( (fromSql tasksid)::Integer )				
							++ " | " ++ 
							fromSql title
							++ " | " ++
							show ((fromSql added)::LocalTime)


Any help is much appreciated, especially as I haven't looked at any Haskell in a while and wasn't any good with it before!

Regards,
Iain

This is my set up:
GHC is 6.10.4
HDBC is HDBC-2.1.1, HDBC-2.2.2, HDBC-postgresql-2.1.0.0,HDBC-postgresql-2.2.0.0
Convertible is convertible-1.0.5, convertible-1.0.8
OSX 10.6


More information about the Haskell-Cafe mailing list