vim-dadbod icon indicating copy to clipboard operation
vim-dadbod copied to clipboard

Feature: Export query result as CSV

Open azthec opened this issue 1 year ago • 4 comments

Something I've noticed in my workflows is the frequent need to export data for others to visualize, although the default query result table is very readable inside a nowrap buffer, it becomes unintelligible when sent to others.

A useful feature for me would be the ability to convert query output into common plaintext formats, such as CSV, and then visualize or save it.

azthec avatar Jul 24 '24 08:07 azthec

It would be nice to have this built as an explicit function (UI trigger) but for now you can simply wrap your query like the following and it will render the csv output into the result pane: Copy (select * from <your query here>) To STDOUT (FORMAT CSV, HEADER, DELIMITER ',');

tompro avatar Aug 02 '24 08:08 tompro

It would be nice to have this built as an explicit function (UI trigger) but for now you can simply wrap your query like the following and it will render the csv output into the result pane: Copy (select * from <your query here>) To STDOUT (FORMAT CSV, HEADER, DELIMITER ',');

Hi, could you please explain how to wrap it? It doesn't seem to be a valid query.

Fr3027 avatar Jan 06 '25 05:01 Fr3027

Sure, the Copy ... To only works with psql (PostgreSQL) there this works as valid export statement. There is a similar non standard export command for mysql that looks like the following: SELECT * FROM table WHERE ... INTO OUTFILE 'name.csv' FIELDS TERMINATED BY ',' ENCLOSED BY '"' LINES TERMINATED BY '\n'; In both cases this is not std SQL but database specific syntax. For both Postgres and MySQL you can find the details for this in their documentation. Depending on the output option (TO or INTO) it will export into the result buffer or a file.

tompro avatar Jan 06 '25 07:01 tompro

Sure, the Copy ... To only works with psql (PostgreSQL) there this works as valid export statement. There is a similar non standard export command for mysql that looks like the following: SELECT * FROM table WHERE ... INTO OUTFILE 'name.csv' FIELDS TERMINATED BY ',' ENCLOSED BY '"' LINES TERMINATED BY '\n'; In both cases this is not std SQL but database specific syntax. For both Postgres and MySQL you can find the details for this in their documentation. Depending on the output option (TO or INTO) it will export into the result buffer or a file.

Thank you for your kind explanations! After searching around, I found that MySQL does not have a function to output CSV content to stdout. Additionally, INTO OUTFILE requires FILE access, which I don't have. As a result, I ended up converting the .dbout file to a CSV file manually using a Python script.

Fr3027 avatar Jan 06 '25 13:01 Fr3027