[Haskell-cafe] HDBC convert [SqlValue] without muchos boilerplate
miguelimo38 at yandex.ru
Thu Feb 11 05:06:09 EST 2010
The problem is, "fromSql x" doesn't know that type it should return. It's sure that it has to be of class "Convertible SqlValue", but nothing more. Could be String, or Int32, or something else.
What if you just omit the "show" function? fromSql seems to be able to convert almost anything to String.
Iain Barnett wrote:
> 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
> 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!
> This is my set up:
> GHC is 6.10.4
> HDBC is HDBC-2.1.1, HDBC-2.2.2, HDBC-postgresql-18.104.22.168,HDBC-postgresql-22.214.171.124
> Convertible is convertible-1.0.5, convertible-1.0.8
> OSX 10.6_______________________________________________
> Haskell-Cafe mailing list
> Haskell-Cafe at haskell.org
More information about the Haskell-Cafe