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

Damien Mattei mattei at oca.eu
Tue Dec 18 10:16:19 UTC 2018


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"}

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:

but for now i have no concrete solution....


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

More information about the Haskell-Cafe mailing list