简单的 SQL 查询结果 - “没有因使用‘查询’而产生 (FromRow Int) 的实例”

问题描述 投票:0回答:1

尝试使用 postgresql-simple 库执行一个非常简单的 sql 查询。

{-# LANGUAGE OverloadedStrings #-}
module Main where

import           Database.PostgreSQL.Simple

main :: IO ()
main = do
  putStrLn "Hello, Haskell!"

f :: Connection -> IO ()
f conn = do
  xs <- query conn "selct 123" ()
  print (xs :: [Int])

错误:

src/Main.hs:12:9-33: error:
    • No instance for (FromRow Int) arising from a use of ‘query’
    • In a stmt of a 'do' block: xs <- query conn "selct 123" ()
      In the expression:
        do xs <- query conn "selct 123" ()
           print (xs :: [Int])
      In an equation for ‘f’:
          f conn
            = do xs <- query conn "selct 123" ()
                 print (xs :: [Int])
haskell postgresql-simple
1个回答
0
投票

处理从查询返回的单个值时,需要使用

Only
数据类型进行换行。所以
xs :: [Int]
应该改为
xs :: [Only Int]

这是解决方法,因为该库使用元组来表示值,而 Haskell 不支持具有单个元素的元组。 https://hackage.haskell.org/package/postgresql-simple-0.6.4/docs/Database-PostgreSQL-Simple.html#g:5

© www.soinside.com 2019 - 2024. All rights reserved.