如何将 SqlByteString 转换为字符串 [HDBC]

how to convert SqlByteString to String [HDBC]

我想在 mysql 上用 hsbc 做点什么,我想知道如何将 Sql ByteString 转换为 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"}

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"