Austin Swinney
2005-Oct-26  07:47 UTC
q: accessing a mysql result set object without the column name
Hi,
I''m writing code to turn a mysql result object into a CSV file.  The  
columns for this result set are not certain as they are  
customizable.  I am looking for some way of iterating over the row''s  
columns of results.  The solution I had in mind was similiar to the  
one for a situation involving an array or a hash.  Of course some  
slick function to join an object ("join()" doesn''t seem to do
that),
or otherwise produce a line of CSV would be nice.
#I''m using this to get my array of objects in the @res:
$sql = "select * from sometable"
@rows = Partition.find_by_sql $sql
@rows.each do | @row |
# and loop through the @row, but this won''t work because @row is not  
an array
     @row.each do | k |
       print @row[k]
     end
end
I can think of several other ways to do this if only I had an array  
and not an object for @row.  A sample output of @row.inspect is:
#"", "ImportGroup"=>"",
"TermsAgree"=>"", "ID"=>"108980",
"IP"=>"10.10.10.1",
"RadioPromo"=>"",
"PalmVersion"=>""}>
** But these result columns are dynamic. They change.**
Thank you for your thoughts!
-Austin
Chris Hall
2005-Oct-26  12:36 UTC
Re: q: accessing a mysql result set object without the column name
try:
rows = Partion.find_by_sql(...)
rows.each do |row|
row.attributes.each { |key,val| puts "#{key} = #{val}" }
end
On 10/26/05, Austin Swinney
<swinney-YXalUvoVQabQT0dZR+AlfA@public.gmane.org>
wrote:>
> Hi,
>
> I''m writing code to turn a mysql result object into a CSV file.
The
> columns for this result set are not certain as they are
> customizable. I am looking for some way of iterating over the
row''s
> columns of results. The solution I had in mind was similiar to the
> one for a situation involving an array or a hash. Of course some
> slick function to join an object ("join()" doesn''t seem
to do that),
> or otherwise produce a line of CSV would be nice.
>
> #I''m using this to get my array of objects in the @res:
> $sql = "select * from sometable"
> @rows = Partition.find_by_sql $sql
>
> @rows.each do | @row |
> # and loop through the @row, but this won''t work because @row is
not
> an array
> @row.each do | k |
> print @row[k]
> end
> end
>
>
> I can think of several other ways to do this if only I had an array
> and not an object for @row. A sample output of @row.inspect is:
>
> #"", "ImportGroup"=>"",
"TermsAgree"=>"", "ID"=>"108980",
> "IP"=>"10.10.10.1 <http://10.10.10.1>",
"RadioPromo"=>"",
> "PalmVersion"=>""}>
>
> ** But these result columns are dynamic. They change.**
>
> Thank you for your thoughts!
>
> -Austin
> _______________________________________________
> Rails mailing list
> Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org
> http://lists.rubyonrails.org/mailman/listinfo/rails
>
_______________________________________________
Rails mailing list
Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org
http://lists.rubyonrails.org/mailman/listinfo/rails