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

Paul aquagnu at gmail.com
Tue Dec 18 10:59:57 UTC 2018


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
>



More information about the Haskell-Cafe mailing list