[Haskell-cafe] [ANN] mysql-simple - your go-to package for talking to MySQL

Christopher Done chrisdone at googlemail.com
Tue Jun 21 16:15:29 CEST 2011

On 21 June 2011 13:45, David Virebayre <dav.vire+haskell at gmail.com> wrote:
> The very first example didn't work for me :
> ------------------------
> {-# LANGUAGE OverloadedStrings #-}
>  import Database.MySQL.Simple
>  hello = do
>   conn <- connect defaultConnectInfo
>   query conn "select 2 + 2"

Yeah, the first example is wrong. Indeed. Should be fixed. I forgot to
tell Bryan about that. I noticed it was wrong when I first looked at
it but it's still tripping people up.

You can access the docs on a slightly earlier version:

> Next I modified the simple example to call a stored procedure, it
> returns a resultset of 12 columns.
> Unfortunately, I realised that QueryResults instances are defined up
> to 10 elements only.
> However, the documentation shows how to define a QueryResults
> instance, so I created a datatype and tried to define the instance,
> and got stuck with an error:
>    Couldn't match expected type `PlateauSel'
>           against inferred type `Int -> a'
>    In the expression: convertError fs vs
>    In the definition of `convertResults':
>        convertResults fs vs = convertError fs vs
>    In the instance declaration for `QueryResults PlateauSel'
> Indeed, the documentation shows that convertError takes 3 parameters,
> and I gave, as per the example, only 2.
> But I'm not sure what to write for the 3rd parameter, the
> documentation doesn't help me here.

The doc specifies it here:

> convertError :: [Field] -> [Maybe ByteString] -> Int -> a
> Throw a ConversionFailed exception, indicating a mismatch between the number of columns in the Field and row, and the number in the collection to be converted to.

So if you're making an instance for a type that takes ten items from
the collection, then put 10. Could always make this clearer.

> To try, I put 0, and the test compiled. However, I had a connection
> error number 1312, saying my procedure "can't return a result set in
> the given context". (The query I used works from the mysql
> command-line interface)

Ah, I wouldn't know about that, I haven't used the mysql version.

More information about the Haskell-Cafe mailing list