Send Beginners mailing list submissions to
        beginners@haskell.org

To subscribe or unsubscribe via the World Wide Web, visit
        http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners
or, via email, send a message with subject or body 'help' to
        beginners-requ...@haskell.org

You can reach the person managing the list at
        beginners-ow...@haskell.org

When replying, please edit your Subject line so it is more specific
than "Re: Contents of Beginners digest..."


Today's Topics:

   1. Re:  database access,extracting result... (Damien Mattei)
   2. Re:  database access,extracting result... (David McBride)


----------------------------------------------------------------------

Message: 1
Date: Mon, 3 Dec 2018 15:40:39 +0100
From: Damien Mattei <mat...@oca.eu>
To: beginners@haskell.org
Subject: Re: [Haskell-beginners] database access,extracting result...
Message-ID: <5c054067.3090...@oca.eu>
Content-Type: text/plain; charset=utf-8

just find myself this solution:

main :: IO ()
main =

  do
    conn <- connect defaultConnectInfo
      { connectHost = "moita",
        connectUser = "mattei",
        connectPassword = "****",
        connectDatabase = "sidonie" }

    rows <- query_ conn "select `N° BD` from sidonie.Coordonnées where
Nom = 'A    20'"


    forM_ rows $ \(Only a) ->
      putStrLn $  Text.unpack a

Le 03/12/2018 12:21, Damien Mattei a écrit :
> {-# LANGUAGE OverloadedStrings #-}
> 
> import Database.MySQL.Simple
> import Control.Monad
> import Data.Text as Text
> import Data.Int
> 
> 
> main :: IO ()
> main = do
>   conn <- connect defaultConnectInfo
>     { connectHost = "moita",
>       connectUser = "mattei",
>       connectPassword = "******",
>       connectDatabase = "sidonie" }
> 
>   rows <- query_ conn "select `N° BD` from sidonie.Coordonnées where Nom
> = 'A    20'"
> 
>   --putStrLn $ show rows
> 
>   forM_ rows $ \(fname, lname) ->
>      putStrLn $  fname ++ " " ++ Text.unpack lname ++ " "
> 
> 
> here is the error:
> 
> *Main> :load Toto
> [1 of 1] Compiling Main             ( Toto.hs, interpreted )
> Ok, one module loaded.
> *Main> main
> *** Exception: ConversionFailed {errSQLType = "1 values:
> [(VarString,Just \"-04.3982\")]", errHaskellType = "2 slots in target
> type", errFieldName = "[\"N\\194\\176 BD\"]", errMessage = "mismatch
> between number of columns to convert and number in target type"}
> 
> -04.3982 is the values i want to put in a variable,it's the N° BD
> (Durchmusterung Number)
> 
> 
> ???
> 
> any help greatly appeciated ;-)
> 
> 

-- 
damien.mat...@unice.fr, damien.mat...@oca.eu, UNS / OCA / CNRS


------------------------------

Message: 2
Date: Mon, 3 Dec 2018 09:42:11 -0500
From: David McBride <toa...@gmail.com>
To: Haskell Beginners <beginners@haskell.org>
Subject: Re: [Haskell-beginners] database access,extracting result...
Message-ID:
        <CAN+Tr43X5L6sQ7nxQhuxfMWXdh1610MEeqhb8XTsBY_b6Ua=o...@mail.gmail.com>
Content-Type: text/plain; charset="utf-8"

Here's how it works, every time you supply a query, you supply the
parameters to the query in a tuple.  Furthermore the results can be gotten
back as a tuple by type hinting each value.  Warning:  I have not run this
code, but it should be close.

query "select age, is_old from user where uid = ? and name = ? (uid :: Int,
name :: String)  :: IO [(Integer, Bool)]

But what happens when you want to supply a single parameter (or receive a
single value?)

query "select * from user where uid = ?" (uid)

The problem with that is (uid) is not a tuple.  It's just an integer in
parenthesis.  There in fact is no way to specify a tuple of one length.  So
mysql-simple (and other libraries) very often have a single type meant to
be used as a single element tuple.  In mysql-simple's (and
postgresql-simple) case that is the (Only a) type.  (Side note, I wish
these were in the standard Tuple module, as this comes once in awhile).

query "select name from user where uid = ?" (Only uid) :: IO [Only String]

Remember that you can also make your own records implement the QueryParams and
QueryResults classes so that you can write

On Mon, Dec 3, 2018 at 6:22 AM Damien Mattei <mat...@oca.eu> wrote:

> {-# LANGUAGE OverloadedStrings #-}
>
> import Database.MySQL.Simple
> import Control.Monad
> import Data.Text as Text
> import Data.Int
>
>
> main :: IO ()
> main = do
>   conn <- connect defaultConnectInfo
>     { connectHost = "moita",
>       connectUser = "mattei",
>       connectPassword = "******",
>       connectDatabase = "sidonie" }
>
>   rows <- query_ conn "select `N° BD` from sidonie.Coordonnées where Nom
> = 'A    20'"
>
>   --putStrLn $ show rows
>
>   forM_ rows $ \(fname, lname) ->
>      putStrLn $  fname ++ " " ++ Text.unpack lname ++ " "
>
>
> here is the error:
>
> *Main> :load Toto
> [1 of 1] Compiling Main             ( Toto.hs, interpreted )
> Ok, one module loaded.
> *Main> main
> *** Exception: ConversionFailed {errSQLType = "1 values:
> [(VarString,Just \"-04.3982\")]", errHaskellType = "2 slots in target
> type", errFieldName = "[\"N\\194\\176 BD\"]", errMessage = "mismatch
> between number of columns to convert and number in target type"}
>
> -04.3982 is the values i want to put in a variable,it's the N° BD
> (Durchmusterung Number)
>
>
> ???
>
> any help greatly appeciated ;-)
>
>
> --
> damien.mat...@unice.fr, damien.mat...@oca.eu, UNS / OCA / CNRS
> _______________________________________________
> Beginners mailing list
> Beginners@haskell.org
> http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: 
<http://mail.haskell.org/pipermail/beginners/attachments/20181203/dc02a11b/attachment-0001.html>

------------------------------

Subject: Digest Footer

_______________________________________________
Beginners mailing list
Beginners@haskell.org
http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners


------------------------------

End of Beginners Digest, Vol 126, Issue 2
*****************************************

Reply via email to