Hello,
My issue is that I am creating a database in MySQL and needing to
export the information in automatically generated reports (.csv), into
a web application written in Rails.
Does anyone have any advice or tutorial links as to how I may do this?
Or whether Rails or MySQL have a preferably freeware application that
could make it much easier?
Thanks
jraglin wrote:
Hello,
My issue is that I am creating a database in MySQL and needing to
export the information in automatically generated reports (.csv), into
a web application written in Rails.
Well, you can generate the CSV files with MySQL’s export feature and
read them with FasterCSV. But that seems silly: it would be better to
have the Rails app connect to the DB directly and get the data it needs.
Does anyone have any advice or tutorial links as to how I may do this?
Or whether Rails or MySQL have a preferably freeware application that
could make it much easier?
Thanks
Best,
Marnen Laibow-Koser
http://www.marnen.org
[email protected]
Hi,
I don’t know if i got you right, but it seems that you are trying to
import your mysql data into csv report?
Well, the easiest way to do this is using the fastercsv gem.
- Install gem : sudo gem install fastercsv
- Add on top of your controller : require ‘fastercsv’
- Create your action and view in your controller :
#The action
def import_csv
result = YourModel.find(:all, :conditions => “your_condition”)
@outfile = “your_csv_report_name_” + Time.now.strftime("%m-%d-%Y") +
“.csv”
csv_data = FasterCSV.generate do |csv|
#field names
csv << [“Field_name_1”]
csv << [“Field_name_2”]
#Data
result.each do |table|
csv << [ table.field1 table.field2]
end
#Write data in csv format
send_data csv_data,
:type => 'text/csv; charset=iso-8859-1; header=present',
:disposition => "attachment; filename=#{@outfile}"
end
redirect_to :action => “your_action”
end
#The view import_csv.html.erb
<% form_tag :action => “import_csv” do %>
<%= submit_tag ‘Print’ %>
<% end %>
Hope this helps!
Regards,