Syntax error, want to use multiple variables in one line, copy mysql data

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. :frowning:

···

--
Posted via http://www.ruby-forum.com/.

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.

  GitHub - brianmario/mysql2: A modern, simple and very fast Mysql library for Ruby - binding to libmysql

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")
=> #<Mysql2::Client:0x9aaff74>
ruby-1.9.2-p0 > results = client.query("SELECT * from people")
=> #<Mysql2::Result:0x9aad558>
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")
=> #<Mysql2::Result:0x9aebf38>
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

···

On Mon, Dec 13, 2010 at 11:35 PM, K. V. <kvaccaro@mitre.org> 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. :frowning:

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

Maybe row looks different than you think. I would try this

require 'pp'
...
while row = res.fetch_row do
  pp row
  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:

This cannot work:

09:32:36 ~$ ruby19 -c <<CODE

while row = res.fetch_row do
    Integer(row[0]), Integer(row[1])
    end
CODE

-:2: syntax error, unexpected ',', expecting keyword_end
    Integer(row[0]), Integer(row[1])
                    ^
09:32:46 ~$

This is the error condensed to the simplest case:

09:32:46 ~$ ruby19 -ce 'a'
Syntax OK
09:33:24 ~$ ruby19 -ce 'a,b'
-e:1: syntax error, unexpected '\n', expecting '='
09:33:27 ~$

You cannot have two expressions separated with comma - unless it's a
list of arguments for a method call.

NoMethodError: undefined method `<' for [0]:Array
from

Probably the code expects a number and not an Array. Whether that's a
bug in your code or in any of the used gems I cannot really tell ATM.

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. :frowning:

Maybe that information is not in sync with the version of the software
you have. Maybe authors of these gems can help.

Kind regards

robert

···

On Mon, Dec 13, 2010 at 11:35 PM, K. V. <kvaccaro@mitre.org> wrote:

--
remember.guy do |as, often| as.you_can - without end
http://blog.rubybestpractices.com/

Thanks for all your help! Turns out the problem was even simpler than I
thought -- one of my outputs was an array not an integer, which flockdb
can't handle, so doing

while row = res.fetch_row do
     flock.add(Integer(row[0]), :follows, Integer(row[1]))
     end

worked fine, though your comments explain why I couldn't check that they
were both integers at the same time! Thanks again!

···

--
Posted via http://www.ruby-forum.com/.