· CSV.ink Team

Generate SQL INSERT Statements from a CSV File

For a few hundred rows, CSV → INSERT is a quick way to seed a table. For large loads, prefer COPY or LOAD DATA — and always escape quotes.

You have a spreadsheet of seed data and an empty table. Generating INSERT statements from CSV is fine for small batches and demos. It gets painful when the file is huge or values contain quotes and backslashes.

The basic shape

INSERT INTO users (id, name, email) VALUES
  (1, 'Ada', 'ada@example.com'),
  (2, 'Lin', 'lin@example.com');

Column order in the INSERT list must match how you map CSV headers. Do not assume the CSV column order equals the table definition.

CSV to SQL INSERT generator →

Escaping and NULL

  • Single quotes inside a string double up in standard SQL: OBrien.
  • Decide whether an empty CSV cell means NULL or an empty string. For foreign keys and optional fields, NULL is usually right; for text comments, empty string may be better.
  • Never paste untrusted CSV into SQL without escaping — that is how injection-shaped accidents happen in hand-built scripts.

Types

CSV has no types. Numbers, dates and booleans are text until you cast them. If your generator quotes every value, you may need to strip quotes for integer columns or cast explicitly in SQL.

When to stop using INSERT files

Above a few thousand rows, prefer native bulk loaders:

  • PostgreSQL: COPY ... FROM STDIN or \copy from a CSV.
  • MySQL: LOAD DATA LOCAL INFILE with a clear column list.
  • SQLite: .import in the CLI.

Bulk loaders are faster, handle escaping more consistently, and keep transactions smaller. Use CSV → INSERT for fixtures, not for migrating a production dump.

Minimal checklist before you run the script

  1. Confirm the target table exists and column types match your values.
  2. Run the first 3–5 generated INSERTs on a staging database.
  3. Check row counts: SELECT COUNT(*) should match the CSV data rows.
  4. Only then run the full file inside a transaction you can roll back.

If any value contains emoji, non-BMP characters or backslashes, verify those rows explicitly. Generators differ in how they escape Unicode.

Связанные инструменты

← Все статьи

Другие статьи