Network.CGI and SQL Queries

Hello there, There's an odd error every time I try to run this code: import Network.CGI import Text.XHtml import qualified Text.Html as H import Database.HDBC import Database.HDBC.Sqlite3 import System.Random ... 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 main = runCGI $ handleErrors cgiTMain 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 Thanks, Will

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:
I'm not 100% sure, but what if you put a liftM or liftIO in somewhere?
(liftM output) . renderHtml $ ...
I think the problem is including that changes cgiTMain to executing in/under another monad; if you specify your types explicitly, it'll help you avoid the changing types problems.

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
participants (3)
-
Alistair Bayley
-
Arlen Cuss
-
william murphy