mysqlhaskellhdbc

how to convert SqlByteString to String [HDBC]


I want do something on mysql with hdbc , I want know how can I convert SqlByteString to String? when I try to use fromSql bytestrobj, I got an error

<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"}

Solution

  • fromSql has the type signature

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

    If you just write fromSql bytestrobj, Haskell cannot tell which type a this expression has. In order to force a = ByteString you can supply the type manually:

    fromSql bytestrobj :: ByteString
    

    Example ghci-session:

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

    If the type can be inferred, then you can drop the type annotation:

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