mygems

Convert a CSV to SQL INSERT statements

A CSV in, a .sql script any database will run.

  • PostgreSQL, MySQL, SQLite
  • Column types detected
  • Quotes escaped correctly
  • Nothing uploaded

1.Open the file

CSV or TSV. Drag it here, or choose a file.

Why it exists

Database import wizards are fussy about CSVs, and command-line loaders need file access the database server often does not have. A plain .sql script works everywhere a query does. Generating it by hand, though, is where quoting goes wrong: one apostrophe in a surname breaks the whole script, and one unescaped backslash corrupts a row in MySQL. This writes the script with the escaping each database actually needs.

What it does

  • Writes a CREATE TABLE statement with a type for each column, decided from every value in it.
  • Writes the rows as multi-row INSERT statements, in batches of a size you choose.
  • Quotes names and escapes values for the database you pick, including MySQL's backslashes.
  • Writes empty cells as NULL, and keeps columns with leading zeros or very long numbers as text.
  • Can wrap everything in a transaction, so a failure part-way leaves the database as it was.

Why batches

One INSERT per row is slow to run, and one INSERT for a million rows is too large for most servers to accept. A few hundred rows per statement is fast and stays under the usual packet and statement-size limits; lower it if your server complains.

Questions

Which databases does it support?
PostgreSQL, MySQL and MariaDB, and SQLite. The scripts are plain SQL, so other databases that follow the standard, such as SQL Server for the INSERT statements, usually accept them with small changes to the CREATE TABLE.
How are the column types chosen?
From every value in the column. A column of whole numbers becomes BIGINT, decimals become a floating-point type, true/false becomes BOOLEAN, ISO dates become DATE, and anything else, including numbers with leading zeros, becomes TEXT. You can edit the CREATE TABLE in the script before running it.
Is it safe to run on a production database?
The script only creates one table and inserts into it. Read it first, as you would any script: the preview shows the start of it, and the table name is yours to choose.