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.