mycli icon indicating copy to clipboard operation
mycli copied to clipboard

Add ability to export tabular data from within mycli

Open mriehl opened this issue 11 years ago • 18 comments

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.

mriehl avatar Jul 31 '15 07:07 mriehl

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

amjith avatar Jul 31 '15 13:07 amjith

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 avatar Jul 31 '15 17:07 brewneaux

@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 avatar Aug 02 '15 18:08 amjith

@amjith @mriehl An update: I got started on this. Just a few more bugs to work out. Hopefully later this week.

brewneaux avatar Aug 03 '15 04:08 brewneaux

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.

amjith avatar Aug 03 '15 05:08 amjith

@brewneaux Thanks for taking a stab at this! Any progress?

tsroten avatar Jan 06 '16 01:01 tsroten

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 csv and then \o foo.csv
  • \T json and 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 avatar Apr 12 '16 04:04 tsroten

@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:

  1. Setup the output format using \T csv, then setup the output file using \o foo.csv and everything after that will go into the file. This will be used for longer sessions.
  2. 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 avatar Apr 12 '16 16:04 amjith

@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?

tsroten avatar Apr 13 '16 02:04 tsroten

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.

amjith avatar Apr 13 '16 02:04 amjith

@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!

mdsrosa avatar Apr 14 '16 19:04 mdsrosa

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).

tsroten avatar Mar 19 '17 01:03 tsroten

#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 set line 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

tsroten avatar May 12 '17 22:05 tsroten

@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.

tsroten avatar Jul 11 '17 14:07 tsroten

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.

ron-premier avatar Apr 22 '19 18:04 ron-premier

@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

damianobarbati avatar Nov 21 '19 13:11 damianobarbati

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

tsroten avatar Dec 20 '19 12:12 tsroten

\T csv ; \o foo.csv ; select * from foo;

ahsar avatar Oct 08 '24 09:10 ahsar