From: Peter Vandenabeele Date: 2010-12-14T08:19:19+09:00 Subject: Re: Syntax error, want to use multiple variables in one line, copy mysql data On Mon, Dec 13, 2010 at 11:35 PM, K. V. wrote: > I'm completely new to Ruby and trying to do something pretty simple -- > run through the lines of a database and copy them using ruby. The data's > going from mysql to flockdb. The meat of the argument looks like this: > > res = mysql.query("SELECT source, target FROM old_data") > while row = res.fetch_row do >     flock.add(row[0], :follows, row[1]) >     end > > Now, I don't know what I'm doing wrong, though I'm sure it's easy, but > this never works. If I use only one of the variables (either row[0] or > row[1]) and make the other some constant (ie flock.add(1, :follows, > row[1]) it works fine. But if I use two, it never works. Even if I want > to check that they're integers, > > while row = res.fetch_row do >     Integer(row[0]), Integer(row[1]) >     end > > it fails. I have checked separately and they are, so that's not the > problem. These are the error messages that come back: > > NoMethodError: undefined method `<' for [0]:Array >  from > /var/lib/gems/1.8/gems/thrift-0.2.0.4/lib/thrift/protocol/binary_protocol.rb:95:in > `write_i64' >  from /var/lib/gems/1.8/gems/thrift-0.2.0.4/lib/thrift/client.rb:35:in > `write' >  from /var/lib/gems/1.8/gems/thrift-0.2.0.4/lib/thrift/client.rb:35:in > `send_message' >  from > /var/lib/gems/1.8/gems/flockdb-0.5.1/lib/flock/gen-rb/flock_d_b.rb:102:in > `send_execute' >  from > /var/lib/gems/1.8/gems/flockdb-0.5.1/lib/flock/gen-rb/flock_d_b.rb:97:in > `execute' >  from > /var/lib/gems/1.8/gems/thrift_client-0.5.0/lib/thrift_client/abstract_thrift_client.rb:115:in > `send' >  from > /var/lib/gems/1.8/gems/thrift_client-0.5.0/lib/thrift_client/abstract_thrift_client.rb:115:in > `handled_proxy' >  from > /var/lib/gems/1.8/gems/thrift_client-0.5.0/lib/thrift_client/abstract_thrift_client.rb:57:in > `execute' >  from > /var/lib/gems/1.8/gems/flockdb-0.5.1/lib/flock/operations/execute_operations.rb:13:in > `apply' >  from /var/lib/gems/1.8/gems/flockdb-0.5.1/lib/flock/client.rb:77:in > `update' >  from /var/lib/gems/1.8/gems/flockdb-0.5.1/lib/flock/client.rb:82:in > `add' >  from (irb):41 >  from /usr/lib/ruby/1.8/rubygems.rb:123 > > What I don't get is why it will add them separately, but won't add both > at the same time! Any help? All the tutorials seem to say this should > work just fine, that multiple variables is not a problem. :( Which library are you using to access the database (how did you generate the mysql variable) ? One way that may be easier is to use the 'mysql2' gem. https://github.com/brianmario/mysql2#readme I just tried a few things and it is really easy: peterv@ASUS:~$ rvm use 1.9.2 Using /home/peterv/.rvm/gems/ruby-1.9.2-p0 peterv@ASUS:~$ gem install mysql2 Building native extensions. This could take a while... Successfully installed mysql2-0.2.6 1 gem installed Installing ri documentation for mysql2-0.2.6... Enclosing class/module 'mMysql2' for class Client not known Enclosing class/module 'mMysql2' for class Result not known Installing RDoc documentation for mysql2-0.2.6... Enclosing class/module 'mMysql2' for class Client not known Enclosing class/module 'mMysql2' for class Result not known peterv@ASUS:~$ irb ruby-1.9.2-p0 > require 'mysql2' => true ruby-1.9.2-p0 > client = Mysql2::Client.new(:host => "localhost", :username => ".....", :password => ".....", :database => "vandenabeele") => # ruby-1.9.2-p0 > results = client.query("SELECT * from people") => # ruby-1.9.2-p0 > results.each do |row| ruby-1.9.2-p0 > puts row.inspect ruby-1.9.2-p0 ?> end.clear {"id"=>1, "first_name"=>"Peter", "last_name"=>"Vandenabeele", "birth_date"=>nil, "gsm_number"=>nil, "comment"=>nil, "created_at"=>2010-10-06 22:20:00 +0200, "updated_at"=>2010-10-06 22:20:00 +0200} => [] ruby-1.9.2-p0 > results = client.query("SELECT * from people") => # ruby-1.9.2-p0 > results.each do |row| ruby-1.9.2-p0 > puts "#{row['first_name']} #{row['last_name']}" ruby-1.9.2-p0 ?> end.clear Peter Vandenabeele => [] HTH, Peter