A robust Python script to convert SQL dump filesβwith advanced support for both MySQL (INSERT) and PostgreSQL (COPY) formatsβinto clean CSV files. Features include improved debugging, reliable multi-line parsing, table structure analysis, optional table filtering, and comprehensive error handling. Effortlessly exports data for further processing or analysis.
- Python 3.6 or newer
-
Clone the repository:
git clone https://github.com/bimantaraz/enhanced-sql-to-csv-converter.git cd enhanced-sql-to-csv-converter
-
Create and activate a virtual environment (recommended):
python3 -m venv venv source venv/bin/activate # On Windows: venv\Scripts\activate
-
Install dependencies:
pip install -r requirements.txt
Convert an SQL dump to CSV with the following command:
python teraz_sql.py input.sql
By default, the output will be input.csv
in the same directory. You can customize settings:
- Specify a custom output file:
python teraz_sql.py input.sql output.csv
- Enable debug mode to see parsing steps and issues:
python teraz_sql.py input.sql -d
- Filter for a specific table (only export rows from one table):
python teraz_sql.py input.sql -t users
- Only analyze the file (show table structure, estimated row counts, etc.):
python teraz_sql.py input.sql --analyze-only
- Works with both MySQL (INSERT INTO) and PostgreSQL (COPY FROM) statements.
- Handles large multi-line statements efficiently.
- Provides progress and error messages during processing.
- For more information or troubleshooting, try adding the
-d
(debug) flag.
python teraz_sql.py sample_dump.sql -d
π Enhanced SQL to CSV Converter
==================================================
π Input file: example.com.sql
π Output file: example.com.csv
==================================================
π Analyzing file structure...
π File Analysis:
π Total lines: 2,529
ποΈ CREATE TABLE statements: 1
π₯ INSERT statements for tables: ['users']
π COPY statements for tables: []
π Estimated data rows: 2,946,547
β
Detected format: MYSQL
π Converting file: example.com.sql
β
Format confirmed: MYSQL
π Header untuk tabel 'users': 51 kolom
β
Conversion completed!
π Total data rows written: 2,126,328
π Output file: example.com.csv
π Output file size: 1,036,956 bytes
π Success! Data converted to example.com.csv
example.com.csv
table_name,id,name,email,created_at,order_id,user_id,total,order_date,product_id,price,category
users,1,John Doe,john@email.com,2023-01-15,,,,,,,,
users,2,Jane Smith,jane@email.com,2023-01-16,,,,,,,,
orders,,,,1,1,99.99,2023-01-20,,,,
orders,,,,2,1,149.50,2023-01-21,,,,
products,,,,,,,1,Widget,29.99,Electronics
products,,,,,,,2,Gadget,49.99,Electronics
python teraz_sql.py sample_dump.sql --analyze-only
π Enhanced SQL to CSV Converter
==================================================
π Input file: example.com.sql
π Output file: example.com.csv
π Debug mode: enabled
π Analyzing file structure...
π DEBUG: Found INSERT for table: users
π File Analysis:
π Total lines: 2,529
ποΈ CREATE TABLE statements: 1
π₯ INSERT statements for tables: ['users']
π COPY statements for tables: []
π Estimated data rows: 2,946,547
β
Detected format: MYSQL
π Converting file: example.com.sql
π DEBUG: Found CREATE TABLE for: users
π DEBUG: Table users columns: ['users', 'email', 'encrypted_password']
π DEBUG: Found INSERT for table: users
β
Format confirmed: MYSQL