如何将 SqlByteString 转换为 String [HDBC]



我想用hdbc在mysql上做点什么,我想知道如何将SqlByteString转换为字符串? 当我尝试使用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"}

fromSql具有类型签名

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

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

fromSql bytestrobj :: ByteString

示例 ghci 会话:

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"

相关内容

  • 没有找到相关文章

最新更新