如何填写使用HDBC的SQL IN占位符的参数? [英] How do you fill in the parameters for an SQL IN placeholder with HDBC?

查看:678
本文介绍了如何填写使用HDBC的SQL IN占位符的参数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

  run c更新问题设置已删除

我试图用Database.HDBC.PostgreSQL做这样的事情:

(?)中的question_id[toSql id]

其中id是[Int] 。但是我得到了错误
$ b $ pre $ $ $ $ $ $没有实例用于(Data.Convertible.Base.Convertible [Int] SqlValue)

你如何使用HDBC填充IN占位符?

解决方案

我不认为你可以避免动态构建查询,但是你仍然可以避免SQL注入等。



<$ p $ (<< $),(< $>))
import Data.List(intersperse)

let query = (
++ intersperse','('?'< $ ids)
++)
in run c query( toSql< $> id)

intersperse < $> < $


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?

解决方案

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

这篇关于如何填写使用HDBC的SQL IN占位符的参数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆