Frank Wang
Frank Wang

Reputation: 1610

How to export sqlite into CSV using RSqlite?

How to export sqlite into CSV using RSqlite? I am asking because I am not familiar with database files, so I want to convert it using R. It may be very simple, but I haven't figure out.

Upvotes: 3

Views: 2445

Answers (2)

frost_wander
frost_wander

Reputation: 91

If the table is not too large, you can first export it into an data frame or matrix in R using the dbGetQuery or the dbSendQuery and fetch commands. Then, you can write that data frame as a .csv file.

my.data.frame <- dbGetQuery(My_conn, "SELECT * FROM My_Table")
write.csv(my.data.frame, file = "MyFileName.csv", ...)

Upvotes: 1

Bhoom Suktitipat
Bhoom Suktitipat

Reputation: 2217

not quite sure if you have figured this out. I am not quite sure how to do it within R either but it seems pretty simple to export to csv using SQLite itself, or by writing out csv from the database you have loaded to R.

In SQLite, you can do something like this at your command prompt

>.mode csv
>.export output.csv
>.header on
>select * from table_name;
>.exit 

SQLite will automatically wrote out your table to a output.csv file

Upvotes: 1

Related Questions