0

编辑:我现在对出了什么问题有了更好的了解。当我以普通的 old 执行该查询时psql,我得到以下结果:

lwm@verbos
=# SELECT * FROM gerund LIMIT1;                 
  infinitive │   gerund    │ gerund_english 
 ────────────┼─────────────┼────────────────
  abandonar  │ abandonando │ abandoning

那么,我要找回 3 个字符串?但是,我说我要回来了IO [Only String]。我敢肯定这是我的类型签名在这里搞砸了......

我正在尝试使用postgresql-simple带有Haskell. 我的代码与我看到的错误一起粘贴在下面。有人有什么想法吗?

我的数据库被调用verbos,在其中,我有一个名为gerund. 我能够运行query_包含:conn "SELECT 2 + 2"并且工作正常。我还可以使用默认信息指定的默认数据连接到我的数据库(密码='postgres':(psql -h localhost -p 5432 -U postgres来自文档[ 1 ])

{-# LANGUAGE OverloadedStrings #-}

module Main where

import Control.Monad
import Control.Applicative
import Database.PostgreSQL.Simple

main = do
    conn <- connect defaultConnectInfo {
        connectPassword = "postgres",
        connectDatabase = "verbos"
    }
    mapM_ print =<< (query_ conn "SELECT * FROM gerund LIMIT 1" :: IO [Only String])

给我以下错误:

ConversionFailed {errSQLType = "3 个值:[(Basic {typoid = Oid 1043, typcategory = 'S', typdelim = ',', typname = \"varchar\"},Just \"abandonar\"),(Basic {typoid = Oid 1043, typcategory = 'S', typdelim = ',', typname = \"varchar\"}, 只是 \"abandonando\"),(基本 {typoid = Oid 1043, typcategory = 'S', typdelim = ' ,', typname = \"varchar\"},Just \"abandoning\")]", errSQLTableOid = Nothing, errSQLField = "", errHaskellType = "1 slot in target type", errMessage = "不匹配的列数在目标类型中转换和编号"}

4

1 回答 1

0

好的,感谢@AlpMestanogullari,@muistooshort,我在这里得到了答案。我的最终代码是:

{-# LANGUAGE OverloadedStrings #-}

module Main where

import Control.Applicative
import Database.PostgreSQL.Simple
import Database.PostgreSQL.Simple.FromRow

data Gerund = Gerund { 
    f :: String, 
    s :: String, 
    t :: String 
} deriving (Show)

instance FromRow Gerund where 
    fromRow = Gerund <$> field <*> field <*> field

main = do
    conn <- connect defaultConnectInfo {
        connectPassword = "postgres",
        connectDatabase = "verbos"
    }
    mapM_ print =<< (query_ conn q :: IO [Gerund])
        where q = "SELECT * FROM gerund LIMIT 1"

笔记:

  • 知道我的结果包含 3 个结果列,我需要为结果定义一个type具有“空格”的值(,fs类型)tGerund
  • 我密切关注 FromRow 的文档 [ 1 ] 以获取我的type定义instance
  • 您需要导入import Database.PostgreSQL.Simple.FromRow才能访问field.
于 2014-11-25T04:44:47.113 回答