CSV to SQL

Convert CSV rows into SQL INSERT statements.

CSV Input
SQL Output
SQL output appears here

Related Tools

Documentation

What is CSV to SQL?

CSV to SQL turns spreadsheet rows into a batch of INSERT INTO statements you can run against an existing table — the fastest way to seed a database or migrate a small export without writing a script.

How it works

The CSV is parsed with papaparse using { header: true, skipEmptyLines: true, dynamicTyping: true }, so the header row supplies column names and cells are coerced to real JS types before generation. The toSqlInserts() function (in src/lib/csvToSql.ts) then builds one INSERT per row: the table name and every column identifier are wrapped in double quotes (with any embedded quote doubled), values are formatted by type — numbers unquoted, booleans as TRUE/FALSE, empty cells as NULL, everything else as a single-quoted string with internal ' doubled to ''. Column order in each statement matches the CSV header order.

Features

  • Editable target table name, remembered in your browser between visits
  • Type-aware value formatting: numbers, booleans, NULL, and quoted strings
  • Quote escaping for both identifiers and string values
  • Copy, download as .sql, or load a .csv file directly

Example

Input (table name: users):

id,name,email,active
1,Alice,alice@example.com,true
2,Bob,bob@example.com,false

Output:

INSERT INTO "users" ("id", "name", "email", "active") VALUES (1, 'Alice', 'alice@example.com', TRUE);
INSERT INTO "users" ("id", "name", "email", "active") VALUES (2, 'Bob', 'bob@example.com', FALSE);

Common errors

This tool only produces INSERT statements — it does not emit a CREATE TABLE, so running the output against a database where the table or columns don't yet exist will fail. The header row is always treated as column names; a headerless CSV needs a synthetic header (e.g. col1,col2,col3) added before pasting. Values are escaped as literal SQL text (single quotes doubled), which is safe against malformed strings breaking the statement, but this is not the same guarantee as a parameterized query — treat the generated SQL as trusted output from your own data, not as a substitute for parameterized queries when handling untrusted user input in an application.

Best practices

Double-check numeric-looking ID or code columns that should stay strings (e.g. zero-padded account numbers) — dynamic typing will convert "007" to the number 7, stripping the leading zeros before it ever reaches SQL. For large CSVs, run the generated INSERTs inside a transaction on the database side so a single failed row doesn't leave the batch partially applied. Double-check the table name field before generating output, since it's baked into every line.

Frequently Asked Questions

How are column types decided?

From each cell's actual value, not a declared schema: numbers become unquoted numeric literals, true/false become SQL TRUE/FALSE, and everything else becomes a quoted string with embedded single quotes doubled ( '' ) for safe escaping. Empty cells become NULL.

Does this create the table first?

No — it only generates INSERT statements assuming the table and columns already exist. Column names are taken directly from the CSV header row.

Is this safe from SQL injection when I run it?

The tool escapes single quotes in string values, but these are still generated as literal SQL text, not parameterized queries — treat pasted CSV data as trusted input (e.g. your own export), not as a substitute for parameterized queries when handling arbitrary user-supplied data in an application.

What if my CSV doesn't have a header row?

The first row is always treated as column names. If your data has no header, add one (e.g. col1,col2,col3) before pasting it in — this tool doesn't currently support a headerless mode.