2

Rubyを使用して、次のようなコマンドを介してシェルからpostgresqlを呼び出しています。

%x[ psql -A -F "," -o feeds/tmp.csv -f lib/sql/query.sql -v id_list="#{id_list}" ]

query.sql は次のようになりますが、変更できます。

Select *
From tbl_test
Where id in (:id_list)

クエリは次のように解決されます。

Select *
From tbl_test
Where id in ('a','b','c')

前もって感謝します。

4

1 に答える 1

2
# before
-v id_list="#{id_list}"

# after
# `join` will separate the values in an array with the string provided
# `map...` the block given to map will surround each item with single quotes
-v id_list="#{id_list.map { |i| "'#{i}'" }.join(', ') }"

# When `id` is an INTEGER you want the `IN` list specified without quotes
# SELECT * FROM tbl_test WHERE id IN (1, 2, 3);
-v id_list="#{id_list.map(&:to_i).join(', ') }"
于 2012-10-13T23:47:48.683 に答える