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

Damien Mattei mattei at oca.eu
Tue Dec 18 15:44:09 UTC 2018


cool...

forM_ bd_rows_WDS $ \(Only x) ->
putStrLn $ "x =" ++ maybe "NULL" Text.unpack x

it worked...
thank you , i will which on i use because i need to forget all NULL
values ,get just an answer

strange lazy evaluation at runtime i had error at this:
putStrLn "before query WDS"
    bd_rows_WDS <- query conn qry_head_WDS (Only (name::String))

 putStrLn "after query WDS"


and it has been solved 20 lines below withe maybe:
forM_ bd_rows_WDS $ \(Only x) ->
putStrLn $ "x =" ++ maybe "NULL" Text.unpack x

that now deal with NULL values

i suppose it's  because until some printing is done nothing is evaluated
before in lazy programming...

Le 18/12/2018 16:22, Neil Mayhew a écrit :
> On 2018-12-18 7:43 AM, Damien Mattei wrote:
>> i hoped asked could avoid using sort of Null pointer as mentioned in
>> this article:
>> https://www.lucidchart.com/techblog/2015/08/31/the-worst-mistake-of-computer-science/
>> if someone else have a simpler solution email me
> 
> You can avoid it: make the DNUM column non-NULL in your database schema.
> That "worst mistake" applies to databases as well as programming
> languages! :-)
> 
> If you have to allow NULL in the database, then you have to handle
> Nothing in your code. However, a neater way of doing it is to use the
> maybe function:
> 
> forM_ bd_rows_WDS $ \(Only x) ->
>       putStrLn $ "x =" ++ maybe "" Text.unpack x
> 
> 
> This allows you to provide a default value in your code for when the
> column is NULL.
> 
> 
> _______________________________________________
> 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