I have this query in SQL used by my Haskell code:
let name = "A 20"
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: [snip]
damien.mattei@gmail.com wrote:
I have this query in SQL used by my Haskell code:
let name = "A 20"
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: [snip]
First, what kind of database are you using and which library are you using
to access it? This may make a difference to the solution.
I think you may be able to get the result you want by indicating to
Haskell's type inference that bd_rows_WDS should have type [Maybe Text], not [Only Text], since there actually are NULLs in your database and you need to be able to handle them. Try replacing your last line with this:
forM_ bd_rows_WDS $ \row -> case row of
Nothing -> putStrLn "The value was NULL"
Just text -> putStrLn $ Text.unpack text
It may also be necessary to explicitly mention the type you expect for bd_rows_WDS:
bd_rows_WDS :: [Maybe Text] <-
query conn qry_head_WDS (Only (name::String))
Hope this helps,
Benjamin
--
Benjamin Esham
https://esham.io
On Tuesday, December 18, 2018 at 7:08:39 PM UTC+1, Benjamin Esham wrote:
damien.mattei@gmail.com wrote:
I have this query in SQL used by my Haskell code:
let name = "A 20"
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: [snip]
[snip]
It may also be necessary to explicitly mention the type you expect for
bd_rows_WDS:
bd_rows_WDS :: [Maybe Text] <-
query conn qry_head_WDS (Only (name::String))
i think it's a good idea Benjamin but unfortunately i get an error i do
not understand from compiler:
*Main> :load UpdateSidonie
[1 of 1] Compiling Main ( UpdateSidonie.hs, interpreted )
UpdateSidonie.hs:213:5: error:
Illegal type signature: ‘[Maybe Text]’
Type signatures are only allowed in patterns with ScopedTypeVariables
|
213 | bd_rows_WDS :: [Maybe Text] <- query conn qry_head_WDS (Only (name::String))
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^
Failed, no modules loaded.
Sysop: | Keyop |
---|---|
Location: | Huddersfield, West Yorkshire, UK |
Users: | 300 |
Nodes: | 16 (2 / 14) |
Uptime: | 57:08:05 |
Calls: | 6,712 |
Files: | 12,243 |
Messages: | 5,355,557 |