[Haskell-cafe] Network.CGI and SQL Queries

Alistair Bayley alistair at abayley.org
Thu Jul 21 03:09:49 CEST 2011


> There's an odd error every time I try to run this code:
>
>
> sqlReaderIO = do
>     handle <- connectSqlite3 "table2.db"
>     scoresSql <- quickQuery' handle "SELECT * FROM pachinkoScores ORDER BY
> score, name" []
>     let scores = map (map (\x -> fromSql x :: String)) scoresSql
>     return (show scores)
>
> ...
>
> cgiTMain = do ...
>               scoresPassed <- sqlReaderIO
>               output . renderHtml $ (inputForm n ss) maybe (inputForm n ss)
> (\mT -> page "Loading..." (visual t v a mT ss) n v a (t + 1) mT) maxT
>
>
> When I remove the "scoresPassed <- sqlReaderIO" line, the code run smoothly.
> Nothing else references "scoresPassed". The ghc, however, gives me this
> error when the line is included:
>
> tankwSql.hs:144:15:
>     No instance for (MonadCGI IO)
>       arising from a use of `output'
>     Possible fix: add an instance declaration for (MonadCGI IO)
>     In the first argument of `(.)', namely `output'
>     In the expression: output . renderHtml
>     In the expression: output . renderHtml $ inputForm n
>
> tankwSql.hs:147:30:
>     Couldn't match expected type `CGIT m0 CGIResult'
>                 with actual type `IO CGIResult'
>     In the first argument of `handleErrors', namely `cgiTMain'
>     In the second argument of `($)', namely `handleErrors cgiTMain'
>     In the expression: runCGI $ handleErrors cgiTMain

You probably need to lift the sqlReaderIO action from IO into CGIT.
liftIO should do it.

You should consider adding type sigs to cgiTMain and sqlReaderIO, so
it's clear which monad you're in for each function.

Alistair



More information about the Haskell-Cafe mailing list