How do you fill in the parameters for an SQL IN pl

2019-07-07 06:41发布

I'm trying to do something like this with Database.HDBC.PostgreSQL:

run c "update questions set deleted = now() where question_id in (?)" [toSql ids]

where ids is [Int]. But I get the error

No instance for (Data.Convertible.Base.Convertible [Int] SqlValue)

How do you fill in an IN placeholder with HDBC?

标签: haskell hdbc
1条回答
等我变得足够好
2楼-- · 2019-07-07 07:13

I don't think you can avoid building the query dynamically, but you can still avoid SQL injection and the like.

import Control.Applicative ((<$), (<$>))
import Data.List (intersperse)

let query = "update questions set deleted = now() where question_id in ("
            ++ intersperse ',' ('?' <$ ids)
            ++ ")"
 in run c query (toSql <$> ids)

Links to documentation for intersperse, <$>, <$.

查看更多
登录 后发表回答