I''m learning how to work with Postgres databases. I''ve figured out how to get the script at http://marcclifton.wordpress.com/2012/11/12/an-example-of-using-postgresql-with-ruby/ to run properly. I learned from this script how to enter data into a database table (addUser function) and how to print data from a database table on the screen (queryUserTable function). What I''m trying to do now is store data from a database table in a variable (like an array of strings). For this particular example, how would you go about doing this? -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To unsubscribe from this group and stop receiving emails from it, send an email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To view this discussion on the web visit https://groups.google.com/d/msg/rubyonrails-talk/-/RAhuzwQT2ZsJ. For more options, visit https://groups.google.com/groups/opt_out.
Jason Hsu, Android developer wrote in post #1103930:> I''m learning how to work with Postgres databases. > > I''ve figured out how to get the script at >http://marcclifton.wordpress.com/2012/11/12/an-example-of-using-postgresql-with-ruby/> to run properly. I learned from this script how to enter data into a > database table (addUser function) and how to print data from a database > table on the screen (queryUserTable function). > > What I''m trying to do now is store data from a database table in a > variable > (like an array of strings). For this particular example, how would you > go > about doing this?p.queryUserTable {|row| printf("%d %s\n", row[''id''], row[''name''])} This line has done exactly what you''re asking. The "row" is a hash variable that contains the data from the database. If you want to put that data a variable of your own the just define a variable and put the data in it: # Add the name from each row in an array my_arrary = [] p.queryUserTable do |row| my_array << row[''name''] end puts my_array In fact if you look at this line: @conn.exec( "SELECT * FROM users" ) do |result| All the data from the table is already given to you inside the "result" hash, which is just a variable containing the data you selected from the database. There''s really no need to make yet another variable to put the data into. Just use the one the PostgreSQL connection gives you back. -- Posted via http://www.ruby-forum.com/. -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To unsubscribe from this group and stop receiving emails from it, send an email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit https://groups.google.com/groups/opt_out.
Are you averse to using ORMs? They tend to make things *so* much easier when dealing with RDBMS databases. Julian On 01/04/2013, at 3:31 PM, "Jason Hsu, Android developer" <jhsu802701@gmail.com> wrote:> I''m learning how to work with Postgres databases. > > I''ve figured out how to get the script at http://marcclifton.wordpress.com/2012/11/12/an-example-of-using-postgresql-with-ruby/ to run properly. I learned from this script how to enter data into a database table (addUser function) and how to print data from a database table on the screen (queryUserTable function). > > What I''m trying to do now is store data from a database table in a variable (like an array of strings). For this particular example, how would you go about doing this? > > -- > You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. > To unsubscribe from this group and stop receiving emails from it, send an email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org > To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org > To view this discussion on the web visit https://groups.google.com/d/msg/rubyonrails-talk/-/RAhuzwQT2ZsJ. > For more options, visit https://groups.google.com/groups/opt_out. > >-- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To unsubscribe from this group and stop receiving emails from it, send an email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit https://groups.google.com/groups/opt_out.