Haskell:使用postgresql.simple(带连接池)获取表计数(*)

时间:2016-03-08 00:15:10

标签: postgresql haskell connection-pooling

请,..如何从一张桌子获得数量(*)?我试图连接一些代码片段,但到目前为止还没有成功。

这就是我所拥有的:

fetchSimple :: FromRow r => Pool Connection -> Query -> IO [r]
fetchSimple pool sql = withResource pool retrieve
   where retrieve conn = query_ conn sql 

getUsersCount :: Pool Connection -> IO (Int)
getUsersCount pool = do
   res <- fetchSimple pool "SELECT count(*) FROM article" :: IO [Int]
   let f = head res
   return f

这给了我以下错误:

No instance for (FromRow Int) arising from a use of ‘fetchSimple’
In a stmt of a 'do' block:
  res <- fetchSimple pool "SELECT count(*) FROM article" :: IO [Int]
In the expression:
  do { res <- fetchSimple pool "SELECT count(*) FROM article" ::
                IO [Int];
       let f = head res;
       return f }
In an equation for ‘getUsersCount’:
    getUsersCount pool
      = do { res <- fetchSimple pool "SELECT count(*) FROM article" ::
                      IO [Int];
             let f = ...;
             return f }

1 个答案:

答案 0 :(得分:0)

如果您只想提取单个值,则不要直接提取。您将其包装在Only

请尝试注释:: IO [Only Int]