CSV to SQL: CREATE TABLE and INSERT Generator
Generate CREATE TABLE and INSERT statements from CSV data.
Use via API
Turn CSV into SQL: an optional CREATE TABLE with inferred column types, then batched INSERT statements, quoted for PostgreSQL, MySQL, SQLite or SQL Server. Send JSON, get JSON back.
POST https://pithlab.com/api/v1/tools/csv-to-sql
curl https://pithlab.com/api/v1/tools/csv-to-sql \
-H "Authorization: Bearer $PITHLAB_KEY" \
-H "Content-Type: application/json" \
-d '{"input":"id,name,active\n1,Ada,true\n2,O'\''Brien,false","tableName":"customers"}'About the CSV to SQL Converter
Turn CSV into CREATE TABLE and INSERT statements for PostgreSQL, MySQL, SQLite or SQL Server, with inferred column types, safe quoting and batched inserts.
How to use the CSV to SQL Converter
- Paste your CSV, or press Example. Keep the header option ticked if the first row holds column names.
- Choose your database and enter a table name, such as customers or public.customers.
- Set how many rows go in each INSERT and whether empty cells should become NULL.
- Check the inferred column types, then copy the SQL or download it as a .sql file to run yourself.
Examples
Input
id,name,active,joined,score 1,Ada,true,2024-01-15,9.5 2,O'Brien,false,,
Output
CREATE TABLE "users" (
"id" INTEGER,
"name" TEXT,
"active" BOOLEAN,
"joined" DATE,
"score" DOUBLE PRECISION
);
INSERT INTO "users" ("id", "name", "active", "joined", "score") VALUES
(1, 'Ada', TRUE, '2024-01-15', 9.5),
(2, 'O''Brien', FALSE, NULL, NULL);Input
sku,price,note A-1,10, B-2,12.5,C:\temp
Output
CREATE TABLE `products` (
`sku` TEXT,
`price` DOUBLE,
`note` TEXT
);
INSERT INTO `products` (`sku`, `price`, `note`) VALUES ('A-1', 10, NULL);
INSERT INTO `products` (`sku`, `price`, `note`) VALUES ('B-2', 12.5, 'C:\\temp');Frequently asked questions
How are column types chosen?
Every non-empty value in a column is checked. Whole numbers become INTEGER (or BIGINT when large), decimals REAL, true and false BOOLEAN, YYYY-MM-DD dates DATE and date-times TIMESTAMP. Anything mixed becomes TEXT. Numbers with leading zeros, like 007 or zip codes, stay TEXT so they are not changed.
How are the types written for each database?
PostgreSQL uses DOUBLE PRECISION and BOOLEAN, MySQL uses INT, DOUBLE and DATETIME, SQL Server uses INT, FLOAT, BIT, DATETIME2 and NVARCHAR(MAX). SQLite stores booleans as 0 and 1 and dates as TEXT.
Is the SQL safe to run with names and values that contain quotes?
Identifiers are always quoted for the dialect (double quotes, backticks or square brackets), with quote characters doubled. Strings double single quotes, MySQL also escapes backslashes, and SQL Server strings use N'...' so Unicode is kept.
What happens to empty cells?
Empty cells in number, boolean and date columns always become NULL. In text columns they become an empty string, unless you tick "Empty cells as NULL".
Does this tool run the SQL or upload my data?
No. It only writes the statements, in your browser. Nothing is executed or sent anywhere, so you can review the SQL before running it yourself.