Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I store the result of a SQL query in a CSV file using Squirrel?

Tags:

Version 3.0.3. It's a fairly large result-set, around 3 million rows.

like image 433
alex Avatar asked Feb 22 '10 20:02

alex


People also ask

Can we run SQL query on CSV file?

Solution. With q [1], conventional SQL queries can be executed on CSVs and other tabular files without connecting to a database or to an SQLite database. If needed, q permits exports to an SQLite database. Q supports all common SQL queries types.


3 Answers

Martin pretty much has this right.

The TL/DR version is that you need the "SQLScripts" plugin (which is one of the "standard' plugins), and then you can select these menu options: Session > Scripts > Store Result of SQL in File

I'm looking at version 3.4. I don't know when this feature was introduced but you may need to upgrade if you don't have and cannot install the SQLScripts plugin.

Instructions for installing a new plugin can be found at: http://squirrel-sql.sourceforge.net/user-manual/quick_start.html#plugins

But if you're performing a fresh install of Squirrel you can simply select the "SQLScripts" plugin during the installation.

Here's the long version:


  1. Run the query

    Connect to the database. Click on the SQL tab. Enter your query. Hit the run button (or Ctrl-Enter).

    You should see the first 100 rows or so in the results area in the bottom half of the pane (depending upon how you've configured the Limit Rows option).

  2. Export the full results

    Open the Session menu. Select the Scripts item (nearly at the bottom of this long menu). Select Store Result of SQL in File.

    This opens a dialog box where you can configure your export. Make sure you check Export the complete result set to get everything.


I haven't tried this with a 3 million row result set, but I have noticed that Squirrel seems to stream the data to disk (rather than reading it all into memory before writing), so I don't see any reason why it wouldn't work with an arbitrarily large file.

Note that you can export directly to a file by using Ctrl-T to invoke the tools popup and selecting sql2file.

like image 187
Rod Avatar answered Oct 07 '22 12:10

Rod


I have found a way to do this, there is a nice support for this in Squirrel. Run the SQL select (the 100 row limit will be ignored by the exporter, don't worry). Then, in the main menu, choose Session, Scripts, Store Result of SQL in File. This functionality may not be present by default, it may be present in some standard plugin (but not installed by default). I don't know which plugin though.

like image 36
Martin Vysny Avatar answered Oct 07 '22 11:10

Martin Vysny


I also wanted to export results of SQL query to CSV file using SquirrelSQL. However according to changes file it seems that this functionality is not supported even in SquirrelSql 3.3.0.

So far I was able to export only data shown in 'result table' of SQL query by right click on the table > Export to CSV. The table size is by default 100 rows and so is the CSV export. You may change the table size in Session Properties > SQL > Limit rows. E.g. change the size to 10000 and your export will also contain 10000 rows. The question is, how will SquirrelSql deal with really big result sets (millions of rows)...

like image 35
Michal Vician Avatar answered Oct 07 '22 12:10

Michal Vician