0

I have a Sqlite table called text that has colums id and text. I want to get these values out for every row from a list of ids, using HDBC and Haskell. In the sqlite3 command-line program, I can run the query: select id, text from text where id in (1.0,8.0); and it works fine. But here's my Haskell code:

-- Takes a single string of comma-delimited ids, 
-- e.g. "1.0,2.0,3.0" representing texts to get. 
getFullText conn ids = do
  stmt <- prepare conn "select id, text from text where id in (?)"
  _ <- execute stmt [toSql ids]
  fetchAllRows stmt

And if I try getFullText conn "1.0" I can get the text for the item with id 1.0. But if I try getFullText conn "1.0,2.0" it just returns [].

I think this is because it expands my query to select text from text where id in ("1.0,8.0"), instead of select text from text where id in (1.0,8.0) or select text from text where id in ("1.0","8.0"). What do I need to do to be able to expand ? into multiple values?

Edit: I see that there's a very similar question here, but as a haskell beginner, I can't figure out what the magical <$> and <$ operators do. Heres's what I've tried:

getFullText conn ids = do
  let query = "select id, text from text where id in (" ++ intersperse ',' ('?' <$ ids) ++ ")"
  stmt <- prepare conn query
  _ <- executeMany stmt ids
  fetchAllRowsAL stmt

But when I try applying this function to a conn and a list of Sqlvalues, I get *** Exception: SqlError {seState = "", seNativeError = -1, seErrorMsg = "In HDBC execute, received [SqlByteString \"105.0\"] but expected 20 args."}. But I think what I'm passing is of length 20, so that should be fine? I think the problem is I don't really understand what <$ is doing.

Jonathan
  • 10,571
  • 13
  • 67
  • 103
  • Possible duplicate of [How do you fill in the parameters for an SQL IN placeholder with HDBC?](https://stackoverflow.com/questions/16472952/how-do-you-fill-in-the-parameters-for-an-sql-in-placeholder-with-hdbc) – beerboy Feb 25 '18 at 22:04
  • What is the type of `ids`, and which value you are supplying for it in your test run? (Also, going by [its description in the docs](https://hackage.haskell.org/package/HDBC-2.4.0.2/docs/Database-HDBC-Statement.html#v:executeMany) I don't think you need `executeMany` here. Clarifying what your `ids` is should also shed some light on that.) – duplode Feb 28 '18 at 23:14

0 Answers0