如何使用 exec_params where params in ('','','')
How to use exec_params where params in ('','','')
我在用
sql = "Select * from table where type in ()
pg_con = PG::Connection.open(myserver)
pg_con.exec_params(sql, [get_type_action])
get_type_action returns 全部输入格式为“'test1'、'test2'、'test3'”等...类型为 0-90 的计数。
所有代码 returns 0 行。我开始寻找问题所在。我输入了一个不带''的值“test1”而不是函数并得到了想要的结果。但是尝试输入多个值“test1、test2”时,我得到 0 行。我做错了什么?
更新 1
我不能为此使用活动记录。但我找到了例子
a = ['alpha', 'bravo', 'ccharlie']
b = $conn.exec_params( %Q{ SELECT id FROM tablename WHERE fieldname IN ( ) }, [ PG::TextEncoder::Array.new.encode(a) ] )
我试过了,但还不行。可以尝试做类似的事情,以示例为基础吗?还是他错了?
PG 不会为您构建 SQL。 in
列表的每个元素都必须是它自己的参数。
sql = "select * from table where type in (, , )"
pg_con = PG::Connection.open(myserver)
p pg_con.exec_params(sql, ["a", "b", "c"]).values
但由于您在 Rails 上使用 Ruby,因此无需编写简单的 SQL。使用 ActiveRecord. Let's say the table name is things. Make an ActivceRecord called Thing
and then call where
和数组作为值。
class Thing < ActiveRecord
end
things = Thing.where(type: ["a", "b", "c"]
我在用
sql = "Select * from table where type in ()
pg_con = PG::Connection.open(myserver)
pg_con.exec_params(sql, [get_type_action])
get_type_action returns 全部输入格式为“'test1'、'test2'、'test3'”等...类型为 0-90 的计数。 所有代码 returns 0 行。我开始寻找问题所在。我输入了一个不带''的值“test1”而不是函数并得到了想要的结果。但是尝试输入多个值“test1、test2”时,我得到 0 行。我做错了什么?
更新 1 我不能为此使用活动记录。但我找到了例子
a = ['alpha', 'bravo', 'ccharlie']
b = $conn.exec_params( %Q{ SELECT id FROM tablename WHERE fieldname IN ( ) }, [ PG::TextEncoder::Array.new.encode(a) ] )
我试过了,但还不行。可以尝试做类似的事情,以示例为基础吗?还是他错了?
PG 不会为您构建 SQL。 in
列表的每个元素都必须是它自己的参数。
sql = "select * from table where type in (, , )"
pg_con = PG::Connection.open(myserver)
p pg_con.exec_params(sql, ["a", "b", "c"]).values
但由于您在 Rails 上使用 Ruby,因此无需编写简单的 SQL。使用 ActiveRecord. Let's say the table name is things. Make an ActivceRecord called Thing
and then call where
和数组作为值。
class Thing < ActiveRecord
end
things = Thing.where(type: ["a", "b", "c"]