[Haskell-cafe] HDBC convert [SqlValue] without muchos boilerplate
iainspeed at gmail.com
Thu Feb 11 04:55:20 EST 2010
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 )
No instance for (convertible-1.0.5:Data.Convertible.Base.Convertible
arising from a use of `fromSql' at TasksSimple.lhs:126:65-73
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 )
++ " | " ++
++ " | " ++
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!
This is my set up:
GHC is 6.10.4
HDBC is HDBC-2.1.1, HDBC-2.2.2, HDBC-postgresql-126.96.36.199,HDBC-postgresql-188.8.131.52
Convertible is convertible-1.0.5, convertible-1.0.8
More information about the Haskell-Cafe