Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature: Export query result as CSV #181

Open
azthec opened this issue Jul 24, 2024 · 4 comments
Open

Feature: Export query result as CSV #181

azthec opened this issue Jul 24, 2024 · 4 comments

Comments

@azthec
Copy link

azthec commented Jul 24, 2024

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.

@tompro
Copy link

tompro commented Aug 2, 2024

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 ',');

@Fr3027
Copy link

Fr3027 commented Jan 6, 2025

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.

@tompro
Copy link

tompro commented Jan 6, 2025

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.

@Fr3027
Copy link

Fr3027 commented Jan 6, 2025

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants