[Haskell-cafe] handling NULL value in database query with Maybe (or other ...)

Damien Mattei mattei at oca.eu
Tue Dec 18 11:14:13 UTC 2018


the URL  of the doc i quoted is :

http://hackage.haskell.org/package/mysql-simple-0.4.5/docs/Database-MySQL-Simple.html

unfortunately there is no developped example to help me
Le 18/12/2018 12:11, Damien Mattei a écrit :
> unfortunately no i'm using  Database.MySQL.Simple
> and the doc mention also:
> 
> Handling null values
> 
> The type of a result tuple will look something like this:
> 
> (Text, Int, Int)
> 
> Although SQL can accommodate NULL as a value for any of these types,
> Haskell cannot. If your result contains columns that may be NULL, be
> sure that you use Maybe in those positions of of your tuple.
> 
> (Text, Maybe Int, Int)
> 
> If query encounters a NULL in a row where the corresponding Haskell type
> is not Maybe, it will throw a ResultError exception.
> 
> but when i put Maybe in my code like this:
> 
> forM_ bd_rows_WDS $ \(Maybe a) ->
>       putStrLn $  Text.unpack a
> 
> i have this error at compilation:
> 
> *Main> :load UpdateSidonie
> [1 of 1] Compiling Main             ( UpdateSidonie.hs, interpreted )
> 
> UpdateSidonie.hs:255:27: error:
>     Not in scope: data constructor ‘Maybe’
>     Perhaps you meant variable ‘maybe’ (imported from Data.Maybe)
>     |
> 255 |     forM_ bd_rows_WDS $ \(Maybe a) ->
>     |                           ^^^^^
> Failed, no modules loaded.
> 
> 
> Le 18/12/2018 11:59, Paul a écrit :
>> If you use Database.Sqlite.Simple library you need to declare returning
>> type as "Maybe XYZ" mandatory., so type will be
>>
>> Only (Maybe String). When I forget about Maybe, I get then same error as
>> you.
>>
>>
>> 18.12.2018 12:16, Damien Mattei wrote:
>>> Hi,
>>>
>>> i have this query in SQL used by my Haskell code:
>>>
>>>      let name = "A    20"
>>>
>>> -- return the list of N°BD from WDS for a given name
>>>      let qry_head_WDS = "select DNUM from WDS where DISC = ?" :: Query
>>>      putStrLn "before query WDS"
>>>      bd_rows_WDS <- query conn qry_head_WDS (Only (name::String))
>>>      putStrLn "after query WDS"
>>>
>>>      forM_ bd_rows_WDS $ \(Only a) ->
>>>        putStrLn $  Text.unpack a
>>>
>>>   works well if there is no NULL values in a database, but if NULL value
>>> for field N°BD i got this exception at runtime:
>>>
>>> before query WDS
>>> *** Exception: UnexpectedNull {errSQLType = "String", errHaskellType =
>>> "Text", errFieldName = "DNUM", errMessage = "unexpected null in table
>>> WDS of database sidonie"}
>>> *Main>
>>>
>>> i need some code ,perheaps uing Maybe to handle the NULL value in the
>>> field N°BD
>>>
>>> i think Haskell have special feature to program this , i read the
>>> article:
>>> https://lukeplant.me.uk/blog/posts/null-pointers-vs-none-vs-maybe/
>>>
>>> but for now i have no concrete solution....
>>>
>>> Regards,
>>> Damien
>>>
>>
>> _______________________________________________
>> Haskell-Cafe mailing list
>> To (un)subscribe, modify options or view archives go to:
>> http://mail.haskell.org/cgi-bin/mailman/listinfo/haskell-cafe
>> Only members subscribed via the mailman list are allowed to post.
> 

-- 
Damien.Mattei at unice.fr, Damien.Mattei at oca.eu, UNS / OCA / CNRS


More information about the Haskell-Cafe mailing list