我有一个名为 Sqlite 的表text
,它有列id
和text
. 我想使用 HDBC 和 Haskell 从 id 列表中为每一行获取这些值。在 sqlite3 命令行程序中,我可以运行查询:select id, text from text where id in (1.0,8.0);
它工作正常。但这是我的 Haskell 代码:
-- 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
如果我尝试getFullText conn "1.0"
,我可以获得 id 为 1.0 的项目的文本。但是,如果我尝试getFullText conn "1.0,2.0"
它只会返回[]
。
我认为这是因为它将我的查询扩展到select text from text where id in ("1.0,8.0")
, 而不是select text from text where id in (1.0,8.0)
or select text from text where id in ("1.0","8.0")
。我需要做什么才能扩展?
到多个值?
编辑:我看到这里有一个非常相似的问题,但是作为一个 haskell 初学者,我不知道魔法<$>
和<$
操作符是做什么的。这是我尝试过的:
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
但是当我尝试将此函数应用于一个conn
和一个 Sqlvalues 列表时,我得到*** Exception: SqlError {seState = "", seNativeError = -1, seErrorMsg = "In HDBC execute, received [SqlByteString \"105.0\"] but expected 20 args."}
. 但我认为我传递的长度为 20,所以应该没问题吗?我认为问题是我真的不明白<$
在做什么。