vim-dadbod
vim-dadbod copied to clipboard
Feature: Export query result as CSV
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.
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 ',');
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.
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.
Sure, the
Copy ... Toonly 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.