1

I am trying to use the fold_ function from Haskell's sqlite-simple package, but how do I call it? I have it in a 'do block' in main, but none of my attempts have been able to compile. I am able to get the query_ function to work, but now I need to retrive the values one at a time. As an example, I am trying to sum a list of integers from the database.

main :: IO ()
main = do
    conn <- open "C:\\project.DB"
    theSum <- fold_ conn "SELECT rserial from conitr1" 0 (\tot val -> val + tot )
    close conn

I understand that it's doing an IO operation, and that I probably need to specify the type somewhere, but nothing I try seems to work. The api documentation is here http://hackage.haskell.org/packages/archive/sqlite-simple/0.4.2.0/doc/html/Database-SQLite-Simple.html#v:fold

4

1 に答える 1

1
main :: IO ()
main = do
    conn <- open "C:\\project.DB"
    theSum <- fold_ conn "SELECT rserial from conitr1" 0 (\tot (Only val) -> return $ (val::Int) + tot )
    close conn

ここで例を見つけましたhttps://github.com/nurpax/db-bench/blob/master/haskell/Sqlite.hs#L24と同じように機能します! 型シグネチャを読むことから始めるのが最適ですが、それだけでは不十分な場合もあります。

于 2013-07-26T23:34:50.683 に答える