MySql::Result#each_hash deletes data

E

eagle eyes joe

Greetings:

You can only read a Mysql::Result object once because
each_key deletes the values as it reads them. This is unexpected and I
think it should be fixed.

This little program shows what I mean:


[Abba:/tmp] josephal% cat resulttest
#!/usr/bin/ruby

require 'mysql'

m = Mysql.new("localhost", "root", "", "Development")

r = m.query("select * from processedfiles limit 5")

r.each_hash { |row| print "#{row['name']}\n" }

print "---------------------------------------\n"

r.each_hash { |row| print "#{row['name']}\n" }

print "------------------- -\nthis is the end\n"


[Abba:/tmp] josephal% resulttest
AD030220
AD030221
AD030228
AD030307
AD000428
 
K

Kent Sibilev

No, this corresponds to the way Mysql C API works. Use
Mysql::Result#row_seek to return to the beginning of the result set.
You shouldn't treat Mysql::Result as a in-memory Enumerable object,
because there are usually resources allocated on the server for each
Mysql::Result instance.

Cheers,
Kent.
 
E

eagle eyes joe

Kent,

Thanks for your help. Are you saying that the data is still there in
the Result object, but I am pointing to the end and I need to rest the
pointer to the beginning? Can you give me an example of using
Result#row_seek?

Thanks again,

Joe.




Kent Sibilev said:
No, this corresponds to the way Mysql C API works. Use
Mysql::Result#row_seek to return to the beginning of the result set.
You shouldn't treat Mysql::Result as a in-memory Enumerable object,
because there are usually resources allocated on the server for each
Mysql::Result instance.

Cheers,
Kent.

Greetings:

You can only read a Mysql::Result object once because
each_key deletes the values as it reads them. This is unexpected and
I
think it should be fixed.

This little program shows what I mean:


[Abba:/tmp] josephal% cat resulttest
#!/usr/bin/ruby

require 'mysql'

m = Mysql.new("localhost", "root", "", "Development")

r = m.query("select * from processedfiles limit 5")

r.each_hash { |row| print "#{row['name']}\n" }

print "---------------------------------------\n"

r.each_hash { |row| print "#{row['name']}\n" }

print "------------------- -\nthis is the end\n"


[Abba:/tmp] josephal% resulttest
AD030220
AD030221
AD030228
AD030307
AD000428
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,581
Members
45,056
Latest member
GlycogenSupporthealth

Latest Threads

Top