Introduction
In this article we will learn how to use ZappyShell Command Line Tools for MySQL CSV Export from Table or Query result
ZappyShell for DB can export your MySQL data to CSV, PDF, Excel in one line command. This is just single exe less than 5 MB.. CSV export allows you to export data in overwite or append mode. You can specify header, escape sequence, qualifiers and some other advanced options for CSV export.
Download here. Now Lets check how to do this.
Download and Install MySQL ODBC Driver
To export data from MySQL we will need ODBC Driver. In this tutorial we have use MySQL ODBC 5.2 Driver but if you are using different driver then change connection string accordingly.
Download and Install MySQL ODBC Driver from below URL
http://dev.mysql.com/downloads/connector/odbc/
Download and Install ZappyShell Command Line Tools
Now next step is we need to Download and Install ZappyShell Command Line tools from hereYou can Download MSI Installer or XCopy Zip file which you can simply extract and copy (see below).
MySQL CSV Export – Single table
c:\tools>db.exe export customers --out c:\data\customers.csv --csv --overwrite --connstr "Driver={MySQL ODBC 5.2 ANSI Driver};Server=your_server_name_or_ip;Port=3306;Uid=user1;pwd=SomeP@ssword;Database=your_db_name"
MySQL CSV Export – Multiple tables
c:\tools>db.exe export customers|orders|products --out c:\data\customers.csv --csv --overwrite --connstr "Driver={MySQL ODBC 5.2 ANSI Driver};Server=your_server_name_or_ip;Port=3306;Uid=user1;pwd=SomeP@ssword;Database=your_db_name"
MySQL CSV Export – Multiple tables (using pattern)
c:\tools>db.exe export cust% --schema Sales --out c:\data\customers.csv --csv --overwrite --connstr "Driver={MySQL ODBC 5.2 ANSI Driver};Server=your_server_name_or_ip;Port=3306;Uid=user1;pwd=SomeP@ssword;Database=your_db_name"
MySQL CSV Export – SQL Query
c:\tools>db.exe export "select * from customers where country like 'US%'" --out c:\data\customers.csv --csv --overwrite --connstr "Driver={MySQL ODBC 5.2 ANSI Driver};Server=your_server_name_or_ip;Port=3306;Uid=user1;pwd=SomeP@ssword;Database=your_db_name"
MySQL CSV Export – Split Files
Sometimes you may have need to split large amount of data into multiple files. With ZappyShell command line you can split data three different ways. Lets check each example.
MySQL CSV Export – Split By Row Count
In below example we are exporting MySQL customer table to CSV files and splitting files with maximum 1000 records per file option.
c:\tools>db.exe export customers --out c:\data\cust.csv --split --splitrows 1000 --connstr "Driver={MySQL ODBC 5.2 ANSI Driver};Server=your_server_name_or_ip;Port=3306;Uid=user1;pwd=SomeP@ssword;Database=your_db_name"
MySQL CSV Export – Split By File Size
In below example we are exporting MySQL customer table to CSV files and splitting files with maximum file size = 10MB option. You also use other size specifier like KB, GB, BYTES etc.
c:\tools>db.exe export customers --out c:\data\cust.csv --split --splitsize 10MB --connstr "Driver={MySQL ODBC 5.2 ANSI Driver};Server=your_server_name_or_ip;Port=3306;Uid=user1;pwd=SomeP@ssword;Database=your_db_name"
MySQL CSV Export – Split By Group By Column
In this example we have exported customers in to separate file by country.
c:\tools>db.exe export customers --out c:\data\cust.csv --groupby Country --connstr "Driver={MySQL ODBC 5.2 ANSI Driver};Server=your_server_name_or_ip;Port=3306;Uid=user1;pwd=SomeP@ssword;Database=your_db_name"
Exporting Compressed CSV files
If you want to output compressed CSV (GZIP format) then use below command.
c:\tools>db.exe export customers --out c:\data\cust.csv --gzip --connstr "Driver={MySQL ODBC 5.2 ANSI Driver};Server=your_server_name_or_ip;Port=3306;Uid=user1;pwd=SomeP@ssword;Database=your_db_name"
Using date time place holders in CSV file name
If you want to use date time place holders in file name so it automatically uses specified date time parts use below command simply putting specifiers in file name anywhere.
c:\tools>db.exe export customers --out c:\data\cust_{{YYYY}}_{{MM}}_{{DD}}_{{HH24}}{{MI}}{{SS}}.csv --connstr "Driver={MySQL ODBC 5.2 ANSI Driver};Server=your_server_name_or_ip;Port=3306;Uid=user1;pwd=SomeP@ssword;Database=your_db_name"