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.
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.
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).
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?
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.
This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.