2013-05-09 25 views
3

我試圖做這樣的事情與Database.HDBC.PostgreSQL:如何用HDBC填充SQL IN佔位符的參數?

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

其中IDS是[INT]。但我得到錯誤

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

你如何填寫一個IN佔位符與HDBC?

回答

2

我不認爲你可以避免動態構建查詢,但你仍然可以避免SQL注入等。

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) 

鏈接文檔intersperse<$><$

+0

你能再解釋一下'<$>'和'<$'的工作原理嗎?這些鏈接似乎已經死亡。 – Jono 2018-02-26 00:05:01

+0

@Jono我更新了鏈接,沒有死。 – dave4420 2018-02-26 21:53:44