Add ability to export tabular data from within mycli
Hi,
I've been using mycli for a few days and I really like it so far. A feature I'd love to see is being able to export tabular data to JSON or CSV, this would allow me to actually get rid of mysql workbench. Is something similar planned or in the works? I can probably find some time to implement the feature myself if it's desired.
I did not have that feature planned in mind. But it will be a great feature to have. If you have the time to implement it, I'd love to take in the contribution.
Let me know if you need some help getting started with the dev setup.
Here's a starter: http://mycli.net/develop
Id also like this - I'm going to see if I can hack it together this weekend, probably as a pager implementation or a slash command.
@brewneaux If you'd like to take a stab at it, go for it. I'm swamped with other bug reports so I might not get to this for a while.
@amjith @mriehl An update: I got started on this. Just a few more bugs to work out. Hopefully later this week.
Thank you for the update. I look forward to the PR. :) On Aug 2, 2015 9:37 PM, "brewneaux" [email protected] wrote:
@amjith https://github.com/amjith @mriehl https://github.com/mriehl An update: I got started on this. Just a few more bugs to work out. Hopefully later this week.
— Reply to this email directly or view it on GitHub https://github.com/dbcli/mycli/issues/91#issuecomment-127120633.
@brewneaux Thanks for taking a stab at this! Any progress?
Ok, I'd like to implement this one as I would use it a lot. The biggest hurdle I'm seeing is the interface for this feature.
We could enable it via a command that would append future output to the file (or it could export the previous query's output).
\export csv foo.csv\export json foo.json\export xlsx foo.xlsx
We could do format-specific commands, e.g.:
\csv foo.csv select * from foo;\json foo.json select * from foo;\xlsx foo.xlsx select * from foo;
Or, we could do a single command that takes a format, filename, and SQL command (\o or \x)
\o csv foo.csv select * from foo;\o json foo.json select * from foo;\o xlsx foo.xlsx select * from foo;
Or, we could set the table type and then send output to a file. In this case, you could output any of the supported table types.
\T csvand then\o foo.csv\T jsonand then\o foo.json
You can see some examples for a different db cli here: https://www.sqlite.org/cli.html
@amjith @mdsrosa Any other ideas?
@tsroten There's actually a current PR pending for pgcli that implements something similar.
https://github.com/dbcli/pgcli/pull/483
My preference would be this:
- Setup the output format using
\T csv, then setup the output file using\o foo.csvand everything after that will go into the file. This will be used for longer sessions. - For a temporary run where I want to just dump the output of a single command, I could use: \csv foo.csv select * from foo;
What do you think?
@amjith I like that idea.
Without adding any dependencies, we can get these formats pretty easily (not sure if we even want all these export options, just simply pointing them out:
- CSV
- JSON
- TSV
- LaTeX
- HTML
If we introduce a dependency on Tablib, we'd be able to standardize the export code to include the above formats in addition to these:
- YAML
- dBASE (DBF)
- ODS
- XLSX
- XLS (limit to 65,000 rows)
In my mind, CSV/JSON is pretty much essential. YAML and XLSX support would be particularly nice for a modern database client, but it does add to our dependencies. I'm not even sure if people need access to XLS since XLSX has superseded it. ODS is also nice, but not essential.
So, I'm thinking about creating optional dependencies (not installed by default). By default, with no extra package installations, we'll support CSV, JSON, TSV, HTML, and LaTeX -- all out of the box. Many of those are a feature of our current tabulate library. If you have Tablib installed with the other necessary dependencies, then you can export to (not output via stdout) YAML, ODS, and XLSX.
What do you think?
I don't have anything against adding tablib as a dependency, as long as it is pure python. If it has a C extension then its a challenge to install on windows.
@tsroten @amjith Sorry for taking so long to respond. We're launching a new version of our product at the company I work for, so things has been crazy around here. 😁
Those are great ideas! But I don't see any advantages in exporting to HTML and TSV. In the other hand, CSV, JSON and LaTeX would be awesome!
On a related note: mycli 1.9.0 was just released which includes the ability to export to CSV from batch mode (not interactive mode).
#433 Implements saving output to a file. Coupled with the output formats CSV and TSV, mycli can now export tabular data (in the pending release).
Once it's released, I'll close this issue. We can open a separate issue(s) for a few remaining points:
- the output has the
N rows in setline in it - the output is also outputted to the screen -- there should be a way to export without displaying the data
- we don't support other formats like JSON
@mriehl @brewneaux This is included as of version 1.11.0. If you installed via pip, you can upgrade by running pip install --upgrade mycli.
You can now save CSV and TSV output to a file like this:
\T csv
\o ~/my_file.csv
select * from users where created_at > '2017-01-01';
The \o (\once) command saves the next command's output to a file. For longer sessions you can look at mycli's tee command.
I'm surprised that you get exactly one shot with \o filename.csv. You must exit mycli before the command will write a file again.
@tsroten thanks! can this be done without entering the interactive shell? I'm currently doing but I don't know how to integrate does commands from cli:
mycli ..... < query.sql > output.csv
Hello @damianobarbati I think if you pass the --csv option to mycli on the command-line, then the non-interactive output will be in CSV format. I hope that helps!
You can find that option and others by running mycli --help
\T csv ; \o foo.csv ; select * from foo;