ruby oci8如何使用值数组作为绑定值

时间:2012-08-23 07:37:35

标签: ruby oracle rhomobile oci8

我想在数组或散列中使用可变数量的值,以便在RhoMobile的RhoConnect部分中使用oci8作为动态生成的sql更新字符串的绑定变量。不知道如何这样做:这是我的irb摆弄。

b(main):006:0> values = ['1','2']
b(main):007:0>  @conn.exec( 'Select id, age, salary,name,company,gender from employee where id = :1 or id = :2', values  ) do |row|
b(main):008:1* puts row[0]
b(main):009:1> end
ntimeError: unsupported datatype: 2
      from C:/RhoStudiotest/ruby/lib/ruby/gems/1.8/gems/ruby-oci8-2.1.2-x86-mingw32/lib/oci8/oci8.rb:650:in `make_bind_object'
      from C:/RhoStudiotest/ruby/lib/ruby/gems/1.8/gems/ruby-oci8-2.1.2-x86-mingw32/lib/oci8/oci8.rb:454:in `bind_param'
      from C:/RhoStudiotest/ruby/lib/ruby/gems/1.8/gems/ruby-oci8-2.1.2-x86-mingw32/lib/oci8/oci8.rb:671:in `bind_params'
      from (irb):9:in `each_with_index'
      from C:/RhoStudiotest/ruby/lib/ruby/gems/1.8/gems/ruby-oci8-2.1.2-x86-mingw32/lib/oci8/oci8.rb:669:in `each'
      from C:/RhoStudiotest/ruby/lib/ruby/gems/1.8/gems/ruby-oci8-2.1.2-x86-mingw32/lib/oci8/oci8.rb:669:in `each_with_index'
      from C:/RhoStudiotest/ruby/lib/ruby/gems/1.8/gems/ruby-oci8-2.1.2-x86-mingw32/lib/oci8/oci8.rb:669:in `bind_params'
      from C:/RhoStudiotest/ruby/lib/ruby/gems/1.8/gems/ruby-oci8-2.1.2-x86-mingw32/lib/oci8/oci8.rb:473:in `exec'
      from C:/RhoStudiotest/ruby/lib/ruby/gems/1.8/gems/ruby-oci8-2.1.2-x86-mingw32/lib/oci8/oci8.rb:282:in `exec_internal'
      from C:/RhoStudiotest/ruby/lib/ruby/gems/1.8/gems/ruby-oci8-2.1.2-x86-mingw32/lib/oci8/oci8.rb:275:in `exec'
      from (irb):7
b(main):010:0>

1 个答案:

答案 0 :(得分:2)

您可以执行以下操作 -

sql = "Select id, age, salary,name,company,gender from employee where id = :1 or id = :2"
index,values = 1,["1","2"]

cursor = conn.parse(sql)
values.each {|value| cursor.bind_param(index,value);index += 1}
cursor.exec

while row = cursor.fetch
    puts "#{row}"
end