2012-05-31 8 views
9

यहाँ कोड मैं का उपयोग कर रहा है:मैं एक MySQL परिणाम सेट के माध्यम से कैसे पुन: प्रयास कर सकता हूं?

# Run the query against the database defined in .yml file. 
# This is a Mysql::result object - http://www.tmtm.org/en/mysql/ruby/ 
@results = ActiveRecord::Base.connection.execute(@sql_query) 

मेरे विचार में, यहाँ है कि मैं क्या मान देखने के लिए कैसे करते है:

<pre><%= debug @results %></pre> 
Outputs: #<Mysql2::Result:0x007f31849a1fc0> 

<% @results.each do |val| %> 
    <%= val %> 
<% end %> 
Outputs: ["asdfasdf", 23, "qwefqwef"] ["sdfgdsf", 23, "asdfasdfasdf"] 

तो कल्पना मैं select * from Person की तरह कुछ क्वेरी, और कहा कि एक रिटर्न परिणाम सेट जैसे:

ID  Name  Age 
1  Sergio 22 
2  Lazlow 28 
3  Zeus  47 

मैं प्रत्येक मूल्य के माध्यम से कैसे पुन: प्रयास कर सकता हूं और इसे आउटपुट कर सकता हूं?

यहां प्रलेखन उपयोगी नहीं है क्योंकि मैंने उन तरीकों का प्रयास किया है जो माना जाता है, लेकिन दुभाषिया मुझे यह कहते हुए एक त्रुटि देता है कि ये विधियां मौजूद नहीं हैं। क्या मैं गलत दस्तावेज का उपयोग कर रहा हूं?

http://www.tmtm.org/en/mysql/ruby/

धन्यवाद!

उत्तर

23

आप mysql2 मणि ​​का उपयोग कर रहे हैं तो आप mysql2 परिणाम वस्तु हो रही किया जाना चाहिए और डॉक्स के अनुसार आप आप में निम्नलिखित

results.each do |row| 
    # conveniently, row is a hash 
    # the keys are the fields, as you'd expect 
    # the values are pre-built ruby primitives mapped from their corresponding field types in MySQL 
    # Here's an otter: http://farm1.static.flickr.com/130/398077070_b8795d0ef3_b.jpg 
end 

चेकआउट प्रलेखन करने के लिए here

तो सक्षम होना चाहिए आप होने लगते हैं: मामले आप निम्नलिखित

<% @results.each do |val| %> 
    <%= "#{val['id']}, #{val['name']}, #{val['age']}" %> 
<% end %> 

संपादित कर सकते हैं गलत दस्तावेज़ का जिक्र करते हुए MySQL 2 रत्न दस्तावेज़ की जांच करें।

+0

धन्यवाद एक गुच्छा! मुझे विश्वास नहीं है कि मैं गलत दस्तावेज़ पढ़ रहा था। :) –

+5

शायद काम पर एक ब्रेक ले लो। – Josnidhin

+1

नाइस ओटर .... – RTF

2

उपयोग :as => :hash:

raw = ActiveRecord::Base.connection.execute(sql) 
raw.each(:as => :hash) do |row| 
    puts row.inspect # row is hash 
end 
+0

'(: as =>: हैश) 'का उपयोग करने का क्या फायदा है और न केवल' .ach'? – Termato

+1

@ टर्माटो मेरे लैपटॉप (विंडोज 7) में, रेल 3, यह हैश के रूप में 'पंक्ति' आउटपुट नहीं करता है, इस प्रकार मैं स्पष्ट रूप से '(: as =>: हैश) जोड़ता हूं। पूरी तरह से सहमत हैं अगर यह काम करता है तो '.each' का उपयोग करें :) – coderz

+0

धन्यवाद, यह बहुत उपयोगी है। – Termato

6

आप ActiveRecord::Base.connection.exec_queryActiveRecord::Base.connection.execute जो

(रेल 3.1+ में उपलब्ध) एक ActiveRecord::Result रिटर्न के बजाय का उपयोग करके देखें, तो फिर तुम .rows, .each की तरह विभिन्न तरीकों से उपयोग कर सकते हैं सकता है या .to_hash

docs से:

result = ActiveRecord::Base.connection.exec_query('SELECT id, title, body FROM posts') 
result # => #<ActiveRecord::Result:0xdeadbeef> 


# Get the column names of the result: 
result.columns 
# => ["id", "title", "body"] 

# Get the record values of the result: 
result.rows 
# => [[1, "title_1", "body_1"], 
     [2, "title_2", "body_2"], 
     ... 
    ] 

# Get an array of hashes representing the result (column => value): 
result.to_hash 
# => [{"id" => 1, "title" => "title_1", "body" => "body_1"}, 
     {"id" => 2, "title" => "title_2", "body" => "body_2"}, 
     ... 
    ] 

# ActiveRecord::Result also includes Enumerable. 
result.each do |row| 
    puts row['title'] + " " + row['body'] 
end 
1

कॉलम हेडर के लिए @ results.fields की तलाश करें।

उदाहरण: @results = [[1, "सर्जियो", 22], [2, "Lazlow", 28], [3, "ज़ीउस", 47]]

@results.fields do |f| 
    puts "#{f}\t" # Column names 
end 

puts "\n" 

@results.each do |rows| # Iterate through each row 
    rows.each do |col| # Iterate through each column of the row 
    puts "#{col}\t" 
    end 
    puts "\n" 
end 

आशा है कि यह है उपयोगी।

संबंधित मुद्दे

 संबंधित मुद्दे