0

My schema:

CREATE TABLE IF NOT EXISTS feed (
  id SERIAL PRIMARY KEY CHECK (id > 0),
  name TEXT NOT NULL,
  url TEXT NOT NULL
);

My code:

{-# LANGUAGE OverloadedStrings #-}

import           Database.PostgreSQL.Simple

hello :: IO String
hello = do
  conn <- connectPostgreSQL ""
  [Only i] <- query_ conn "SELECT url FROM feed WHERE id = 1"
  return i

main :: IO ()
main = hello >>= print

My error:

user error (Pattern match failure in do expression at src/Main.hs:8:3-10)

My questions:

  • How do I fix this error?
  • How could I get a more informative error message?

Update: I connected to the wrong database and therefor my assumption about the result set was wrong. It contained zero lines instead of one line and thus it tried to match [] against [Only i]. I found it out by executing the query in ghci:

:set -XOverloadedStrings
import Database.PostgreSQL.Simple
do;  conn <- connectPostgreSQL "" ;query_ conn "SELECT url FROM feed WHERE id = 1"
Thomas Koch
  • 2,833
  • 2
  • 28
  • 36
  • 1
    What do you get when your use `i <- query_ conn "SELECT url FROM feed WHERE id = 1"`? In that case you can inspect what value you obtained and why it is not matching. – Willem Van Onsem Dec 16 '17 at 16:56
  • if you do `i <- query_ conn "select ..."` change the `hello :: IO String` to `hello :: IO [String]` – epsilonhalbe Dec 16 '17 at 17:08

0 Answers0