Extract a Basic Users Table to CSV
Convert a simple SQL CREATE TABLE and INSERT INTO for a users table into CSV format. Covers column name extraction and basic value parsing.
Detailed Explanation
From SQL to Spreadsheet-Ready Data
The most common use case for SQL to CSV conversion is extracting data from a simple table definition with insert statements. Given a CREATE TABLE and matching INSERT INTO statements, the tool maps column names from the table definition and extracts values from each row tuple.
Example SQL
CREATE TABLE users (
id INTEGER PRIMARY KEY,
name VARCHAR(100) NOT NULL,
email VARCHAR(255) NOT NULL,
age INTEGER
);
INSERT INTO users (id, name, email, age) VALUES
(1, 'Alice', 'alice@example.com', 28),
(2, 'Bob', 'bob@example.com', 35),
(3, 'Charlie', 'charlie@example.com', 22);
Generated CSV
id,name,email,age
1,Alice,alice@example.com,28
2,Bob,bob@example.com,35
3,Charlie,charlie@example.com,22
How It Works
- The parser first reads the
CREATE TABLEstatement to build an ordered list of column names:id,name,email,age. - Each
INSERT INTO ... VALUESrow tuple is parsed, and values are matched positionally to the column list. - String values have their surrounding single quotes removed and escape sequences resolved.
- The output CSV includes a header row by default, followed by one row per data tuple.
This approach preserves the original column ordering defined in the schema, making the CSV predictable and consistent even if the INSERT column list is in a different order.
Use Case
Exporting user data from a database migration script for review in Excel or Google Sheets. Common when auditing seed data or preparing test datasets for QA teams.