How do you fill in the parameters for an SQL IN placeholder with HDBC?

a 夏天 提交于 2019-12-10 17:43:23

问题


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?


回答1:


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, <$>, <$.



来源:https://stackoverflow.com/questions/16472952/how-do-you-fill-in-the-parameters-for-an-sql-in-placeholder-with-hdbc

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!