为什么Haskell不能将posgreSQL字符串分成字段?



我是PostgreSQL的新手。很简单,如果问题很愚蠢,请原谅。

我正在学习这个教程:使用Haskell 的Postgresql数据访问

我得到了运行的第一个演示程序。

现在我使用这个功能:

retrieveClient :: Connection -> Int -> IO [Only String]
retrieveClient conn cid = query conn "SELECT ticker FROM spot_daily WHERE id = ?" $ (Only cid)

并希望将其修改为返回CCD_ 1。

所以我写道:

retrieveClient2 :: Connection -> Float -> IO [(String, Integer, Float)]
retrieveClient2 conn cid =  query conn "SELECT (ticker, timestamp, some_val) FROM spot_daily WHERE p_open > ?" $ (Only cid)
main :: IO ()
main = do
conn <- connect localPG
mapM_ print =<< (retrieveClient2 conn 50.0)

我得到这个:

MyApp-exe.EXE: Incompatible {errSQLType = "record", errSQLTableOid = Nothing, errSQLField = "row", errHaskellType = "Text", errMessage = "types incompatible"}

在哈斯克尔的世界里,说";在Hackage上搜索您想要的类型签名"但从错误消息中我还不清楚什么类型的签名会让GHC满意。

这类东西有转换函数吗?我试过这样做:

data MyStruct = { field1 :: String, field2 :: Integer, field3 :: Float} deriving (Eq, Show)
retrieveClient3 :: Connection -> Int -> IO [Only MyStruct]
retrieveClient3 conn cid = MyStruct (query conn "SELECT ticker FROM spot_daily WHERE id = ?" $ (Only cid))

但这会导致不同的错误。

作为对评论的回应,下面是spot_daily的模式:

Table "public.spot_daily"
Column   |         Type          | Collation | Nullable |                Default
-----------+-----------------------+-----------+----------+----------------------------------------
id        | integer               |           | not null | nextval('spot_daily_id_seq'::regclass)
ticker    | character varying(20) |           | not null |
epoch     | bigint                |           | not null |
p_open    | double precision      |           |          |
p_close   | double precision      |           |          |
p_high    | double precision      |           |          |
p_low     | double precision      |           |          |
synthetic | boolean               |           |          |
Indexes:
"spot_daily_pkey" PRIMARY KEY, btree (id)

PostgreSQL类型区分了;行";和一个";记录";。正如所写的(用括号(,SQL查询返回一条记录,而元组的FromRow实例不处理该记录。

SELECT (ticker, timestamp, some_val) FROM prices_daily WHERE p_open > ?

更改查询(通过删除括号(,使其返回一行,postgresql-simple应该能够处理该行:

SELECT ticker, timestamp, some_val FROM prices_daily WHERE p_open > ?

相关内容

  • 没有找到相关文章

最新更新