Undefined Method `free'

I’ve got a CSV file that I’m trying to read from and insert the values
into a mySQL DB. However whenever I run the script I get the following
error:

csvadd.rb:27: undefined method free' for nil:NilClass (NoMethodError) from csvadd.rb:12:inwith_db’
from csvadd.rb:21

Here is the code I’m using:

def with_db

dbh = Mysql.real_connect(‘localhost’,‘myuser’,‘mypass’,‘mydb’)

begin
yield dbh

rescue MysqlError => error
puts “Error: #{error}”
is nil

ensure
dbh.close
end

end

with_db do |db|

res = db.query(‘load data local infile ‘/full/path/to/csvdoc.csv’
into table mytable fields teminated by ‘,’ lines teminated by ‘\n’
ignore 1 lines’)

res.free

end

I’ve tried searching google, here, ruby docs, other forums, I felt I was
safe asking the question after all that. However if I did miss
something I apologize in advance.

I should also note that if I remove res.free, it enters the info from my
csv file into the DB 3 times.

Mike Keller wrote:
[snip]

However whenever I run the script I get the following
error:
csvadd.rb:27: undefined method free' for nil:NilClass (NoMethodError) from csvadd.rb:12:inwith_db’
from csvadd.rb:21
[snip]
res = db.query(‘load data local infile ‘/full/path/to/csvdoc.csv’
into table mytable fields teminated by ‘,’ lines teminated by ‘\n’
ignore 1 lines’)

res.free
[snip]

What that means is that on line 27, you are calling “free” on a nil
object.
Which means that ‘res’ is a nil object.
Which means that db.query(…) is returning nil.

On 10.01.2007 22:44, Mike Keller wrote:

Gavin K. wrote:

What that means is that on line 27, you are calling “free” on a nil
object.
Which means that ‘res’ is a nil object.
Which means that db.query(…) is returning nil.

My question now is what would someone recommend as the best solution for
this.

Since it’s not a query you do not get anything back. Remove “res”
altogether from the code.

If I simply remove the line with res.free it enters the information into
the db 3 times.

The reason is that the exception will cause a tx rollback. You need to
find out what your DB is doing here.

Am I missing something that is causing it to do this?

Possible reasons that come to mind:

  • data in the file looks different that you think

  • there is already data in the table

  • you have some triggers defined that cause the effect

You could create a test table with the same structure as the one you are
trying to load and test that statement against that table and see what
happens (i.e. what you get in that table).

I’d say Ruby is the wrong place to look into.

Kind regards

robert

Gavin K. wrote:

What that means is that on line 27, you are calling “free” on a nil
object.
Which means that ‘res’ is a nil object.
Which means that db.query(…) is returning nil.

My question now is what would someone recommend as the best solution for
this.

If I simply remove the line with res.free it enters the information into
the db 3 times.

Am I missing something that is causing it to do this?

Thanks.

Robert K. wrote:

On 10.01.2007 22:44, Mike Keller wrote:

Gavin K. wrote:

What that means is that on line 27, you are calling “free” on a nil
object.
Which means that ‘res’ is a nil object.
Which means that db.query(…) is returning nil.

My question now is what would someone recommend as the best solution for
this.

Since it’s not a query you do not get anything back. Remove “res”
altogether from the code.

Thanks guys.

By removing res altogether like Rob said I actually killed two birds
with one stone. When I did of course it got rid of the error as well as
stopping it from throwing the data in the db three times.

If anybody knows exactly why it would do that it would be nice.

Thanks again.