Haskell:使用 postgresql.simple 获取 table count(*)(使用连接池)

Haskell: get table count(*) with postgresql.simple (with Connection Pool)

拜托,..我怎样才能从一个 table 中得到 count(*)?我正在尝试连接一些代码片段,..但到目前为止没有成功。

这是我的:

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 }

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

所以尝试注释 :: IO [Only Int]