Question

I want to use a variable number of values in an array or hash to use as bind variables for a dynamically generated sql update string using oci8 in RhoConnect part of RhoMobile. Not sure how to do so though: Here is my irb fiddling.

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>
Was it helpful?

Solution

you can do the following -

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
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top