0

I am currently working on a project and I want to know how to save an sqllite database in rails as a csv file. I want it when you click the button, the current database on the system download. Can anybody help me? Thanks!

1 Answer 1

1

Your problem isn't really specific to Rails. Instead, you're mostly dealing with an administrative issue. You should write a script to export your database as csv, something like this:

#!/bin/bash
./bin/sqlite3 ./my_app/db/my_database.db <<!
.headers on
.mode csv
.output my_output_file.csv
select * from my_table;
!

This script exports a single table. If you have additional tables, you'll want to add them to your script.

The only Rails related issue is the matter of calling that script. Save the script within your application structure; I'd suggest my_app/assets or some similar location.

Now you can run that script using system(command) where command is the absolute path for your script, within a set of double-quotes.

Sign up to request clarification or add additional context in comments.

5 Comments

Look at my answer again, and you'll notice that you missed the part where I wrote: "Save the script within your application structure; I'd suggest my_app/assets or some similar location." If this answer solves your problem, click the checkmark on the left so that others know what worked for you.
how to I get the csv file of my database
@T.Aloufi See the output line in the script I gave you? Specify the output path there. If you don't provide a fully qualified path, you'll get the file wherever you ran the script from.
can you give an example?
@T.Aloufi Teaching you UNIX/Linux is completely out of the scope of Stack Overflow. Why don't you try Google and you can read stuff like this? Honestly, I don't think I can be of any further use to you until you comprehend some of the fundamentals.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.