MySql::Result#each_hash deletes data

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

···

---------------------------------------
------------------- -
this is the end
[Abba:/tmp] josephal%

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.

···

On Sep 25, 2004, at 8:59 PM, eagle eyes joe wrote:

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
---------------------------------------
------------------- -
this is the end
[Abba:/tmp] josephal%

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.

In article <14F23C8D-0F59-11D9-8317-000A95C700E8@bellsouth.net>,

···

Kent Sibilev <ksibilev@bellsouth.net> wrote:

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.

On Sep 25, 2004, at 8:59 PM, eagle eyes joe wrote:

> 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
> ---------------------------------------
> ------------------- -
> this is the end
> [Abba:/tmp] josephal%
>