[Haskell-beginners] database question

Gaius Hammond gaius at gaius.org.uk
Sat Sep 18 16:30:46 EDT 2010


On 18 Sep 2010, at 21:03, Daniel Fischer wrote:

>>
>>
>> Why does it think that that is the final expression of the function?
>
> It doesn't, but you said the do-expression had type
> ReaderT Connection (WriterT [String] m) ()
> while
>
> quickQuery :: IConnection conn =>
>                      conn -> String -> [SqlValue] -> IO [[SqlValue]]
>
> The types don't match, hence the error. But you have a MonadIO  
> constraint,
> so
>
>    rs <- liftIO $ quickQuery ...
>
> should fix it.
>


Aha! It does indeed, thanks :-)



I had assumed that with the rs <- quickQuery I was getting the  
[[SqlValue]] "out" of the IO Monad - but I have to lift it into the IO  
Monad first, since I am in a monadic context? This would be the non- 
monadic version




module Main where

import Database.HDBC
import Database.HDBC.Sqlite3

main:: IO ()
main = do lda <- connectSqlite3 "test.db"
           rs <- quickQuery lda "select datetime ('now')" []

           mapM_ putStrLn (map convRow rs)
             where convRow [x] = (fromSql x)::String

-- EOF



Oh, but I am already in the IO monad there aren't I.... And doQuery  
*isn't* but I have declared that it *should be* (e.g. an instance of  
typeclass MonadIO). Is that accurate?




>> Any advice greatly appreciated. I have been struggling with this  
>> since
>> lunchtime!
>>
>
> Hopefully it didn't stop you from eating.



Nothing could do that :-)



Cheers,




G









More information about the Beginners mailing list