Export data from sqlite to a csv file in rails -
i have app uses sqlite db
, there entries in table. have change sqlite
postgres
. , table design
changed.
if same table design have gone use taps
or dump data using yaml-db
, push data postgres, in scenario table design changes want move data sqlite postgres according new table.
so thought of exporting data sqlite
csv
file , move data csv postgres. way have or there other way doing it? if way how can export csv
, import postgres
?
another thing is, after exporting data sqlite, there way push data through migration
? please me.
to export in csv format add controller action respond_to csv , in matching view folder create .csv.erb file create csv. can called adding .csv url.
so controller this:
def index @people = person.all respond_to |format| format.html # index.html.erb format.json { render json: @people } format.csv end end
and view saved in file (something /app/views/people/index.csv.erb) contain:
first name,last name <% @people.each |p| %> <%= raw "\"#{p.first_name}\"" %>,<%= raw "\"#{p.last_name}\"" %> <% end %>
this way creating csv not dependent on database in use.