postgresql-简单的查询错误 [英] postgresql-simple query error

查看:165
本文介绍了postgresql-简单的查询错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

编辑:我现在有一个更好的想法是什么问题。当我在旧的 psql 中执行该查询时,我得到以下结果:

  lwm @ verbos 
=#SELECT * FROM gerund LIMIT1;
不定式│gerund│gerund_english
────────────┼───────────────────────── ───────
放弃│放弃│放弃

所以, 3串?但是,我说我回来 IO [Only String] 。我确信这是我的类型签名,这是搞砸的事情...

我试图使用 postgresql-简单的库包含 Haskell 。我的代码与我看到的错误一起粘贴在下面。任何人有任何想法?



我的数据库被称为 verbos 并且在其中有一个名为动名词。我能够运行一个查询_ ,它包含: connSELECT 2 + 2,并且工作正常。我也可以使用缺省信息(password ='postgres': psql -h localhost -p 5432 -U postgres (来自docs [ 1
$ b $ pre $ { - #LANGUAGE OverloadedStrings# - }

模块Main其中

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

main = do
conn< - connect defaultConnectInfo {
connectPassword =postgres,
connectDatabase =verbos
}
mapM_ print =<(query_connSELECT * FROM gerund LIMIT 1:: IO [ )

给我以下错误:


ConversionFailed {errSQLType =3 values:[(Basic {typoid = Oid 1043,
typcategory =' S',typdelim =',',typname = \varchar \},只需
\abandonar \),(Basic {typoid = Oid 1043,typcategory ='S',typdelim
=',',typname = \varchar \},Just \abandonando\),(Basic {typoid = Oid 1043,typcategory ='S',typdelim =',',typname =
\varchar \},只是\abandoning \)],errSQLTableOid = Nothing,
errSQLField =,errHaskellType =目标类型中有1个槽位,
errMessage =目标类型转换的列数与
的数目不匹配} b $ b


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

  { - #LANGUAGE OverloadedStrings# - } 

模块Main其中

导入Control.Applicative
导入Database.PostgreSQL.Simple
导入Database.PostgreSQL.Simple.FromRow

数据Gerund = Gerund {
f: :String,
s :: String,
t :: String
}派生(显示)

实例FromRow Gerund其中
fromRow = Gerund< $> ;字段< *>字段< *>字段

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

注意:


EDIT: I now have a better idea of what is going wrong. When I perform that query in plain old psql, I get the following result:

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

So, I am getting back 3 strings? However, I say that I am getting back IO [Only String]. I am sure it is my type signature here that is messing things up ...

I am trying to make a simple query using the postgresql-simple library with Haskell. My code is pasted below along with the error I am seeing. Anyone got any ideas?

My database is called verbos and within it, I have a table called gerund. I am able to run a query_ that contains: conn "SELECT 2 + 2" and that works fine. I can also connect to my database with the default data as specified with the default information (password = 'postgres' : psql -h localhost -p 5432 -U postgres (from the docs[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])

Gives me the following error:

ConversionFailed {errSQLType = "3 values: [(Basic {typoid = Oid 1043, typcategory = 'S', typdelim = ',', typname = \"varchar\"},Just \"abandonar\"),(Basic {typoid = Oid 1043, typcategory = 'S', typdelim = ',', typname = \"varchar\"},Just \"abandonando\"),(Basic {typoid = Oid 1043, typcategory = 'S', typdelim = ',', typname = \"varchar\"},Just \"abandoning\")]", errSQLTableOid = Nothing, errSQLField = "", errHaskellType = "1 slots in target type", errMessage = "mismatch between number of columns to convert and number in target type"}

解决方案

OK, Thanks to @AlpMestanogullari, @muistooshort, I got an answer here. My final code is:

{-# 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"

Notes:

  • Knowing that my result contained 3 result columns, I needed to define a type that had 'space' for the results (f, s and t in the Gerund type)
  • I followed the docs[1] for FromRow closely to get my type and instance defined.
  • You need to import import Database.PostgreSQL.Simple.FromRow to access things like field.

这篇关于postgresql-简单的查询错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆