如何将SqlByteString转换为String [HDBC]

时间:2018-01-31 09:09:21

标签: mysql haskell hdbc

我想用hdbc在mysql上做点什么,我想知道如何将SqlByteString转换为String?当我尝试使用fromSql bytestrobj时,我收到了错误

<interactive>:20:1: error:
    • Non type-variable argument
        in the constraint: Data.Convertible.Base.Convertible SqlValue a
      (Use FlexibleContexts to permit this)
    • When checking the inferred type
        it :: forall a. Data.Convertible.Base.Convertible SqlValue a => a
*InitPriceTable Database.HDBC Database.HDBC.Types> conn<-connectMySQL defaultMySQLConnectInfo {mysqlUser ="root",mysqlPassword ="root",mysqlDatabase ="linclon"}

1 个答案:

答案 0 :(得分:0)

fromSql具有类型签名

fromSql :: Convertible SqlValue a => SqlValue -> a

如果您只是编写fromSql bytestrobj,Haskell无法分辨此表达式具有哪种类型a。为了强制a = ByteString,您可以手动提供类型:

fromSql bytestrobj :: ByteString

示例ghci-session:

Prelude> :m +Database.HDBC Data.ByteString
Prelude Database.HDBC Data.ByteString> fromSql (toSql "Hello") :: ByteString 
"Hello"

如果可以推断出类型,则可以删除类型注释:

Prelude Data.ByteString Database.HDBC> Data.ByteString.take 4 (fromSql (toSql "Hello"))
"Hell"