[英]how to insert an array of strings to sql query in ruby
I have this query in ruby: 我在ruby中有以下查询:
sql = "SELECT variants.id,
code,
regular_price,
price_before_sale
FROM variants
WHERE variants.code IN (#{context.codes.join(",")})"
where context.codes = ['PRDCT-1','PRDCT-2']
其中
context.codes = ['PRDCT-1','PRDCT-2']
now context.codes becomes (PRDCT1,PRDCT2)
inside the sql query because of the .join
but what I want to happen is ('PRDCT1','PRDCT2')
what am I missing? 现在context.codes由于
.join
而成为sql查询中的(PRDCT1,PRDCT2)
,但是我想发生的是('PRDCT1','PRDCT2')
我想念的是什么?
EDI: I have tried to do (#{context.codes.join("','")})
but it returns (PRDCT1','PRDCT2)
EDI:我尝试做
(#{context.codes.join("','")})
但返回(PRDCT1','PRDCT2)
Don't do that. 不要那样做 Bobby Tables is watching.
Bobby Tables在看着。 Instead, provide the adequate number of placeholders:
而是提供足够数量的占位符:
sql = "SELECT variants.id,
code,
regular_price,
price_before_sale
FROM variants
WHERE variants.code IN (#{context.codes.map { "?" }.join(",")})"
and then provide *context.codes
in statement parameters. 然后在语句参数中提供
*context.codes
。
I got it. 我知道了。 I added single quotes to
('#{context.codes.join("','")}')
我在
('#{context.codes.join("','")}')
添加了单引号
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.