Developer tools

CSV to SQL converter

This CSV to SQL converter turns the rows of a CSV file into INSERT statements for SQLite, PostgreSQL or MySQL, with an optional CREATE TABLE. Every value stays text unless you set its column to Decimal, so leading zeros and codes keep their form. The conversion runs in your browser and saves a .sql file.

Files stay in your browser

1. Input CSV

Paste CSV or choose a UTF-8 file.

Enable JavaScript to convert CSV.

2. SQL settings

Columns

Read columns, then edit names and types. Text is the default; Decimal is an explicit conversion.

No columns loaded yet.

How to convert CSV to SQL

  1. Read the CSV columns

    Paste CSV or select Choose CSV file. Check Delimiter and First row contains headers, then select Read columns.

  2. Set the SQL table

    Choose the Database type and Table name. Edit column names, keep Text for codes, or choose Decimal for numeric values.

  3. Preview or download SQL

    Select Preview SQL to check the statements or Download .sql to save converted.sql. Copy SQL copies the complete output.

Paste CSV into the CSV data box, or select Choose CSV file to open a UTF-8 .csv, .tsv or .txt file. Delimiter starts on Auto-detect, which picks comma, semicolon, tab or pipe, and First row contains headers is on. Select Read columns to list every column.

Under SQL settings, pick SQLite, PostgreSQL or MySQL as the Database type, set the Table name, and edit each column name and type. Select Preview SQL to read the output on the page, or Download .sql to save converted.sql. Copy SQL puts the complete output on the clipboard. An edit to the CSV clears the columns, and a change to a setting clears the old output, so generate again after each change.

Example: the three-row sample

Load example reads a CSV with the columns id, name and note and 3 data rows. With the default settings, SQLite and a table named data, the output has 2 statements: a CREATE TABLE with three TEXT columns and one INSERT that carries all three rows.

The first row becomes ('001', 'Zoë', 'O''Brien, 東京'). The id keeps its leading zeros because it stays text, the apostrophe in O'Brien is doubled, and the comma and the Japanese characters stay inside one value. The second note holds a line break inside quotes. SQLite keeps it as a real line break, PostgreSQL writes it as \n inside an E'' string, and MySQL writes that value as a hex string.

Sam has an empty note, so the last row ends with '' rather than NULL. Run in SQLite, the output creates a table whose rows read back exactly as the CSV cells.

Text and Decimal columns

Every column starts as Text (preserve). Text writes each cell as a SQL string, so 007, +1 and 2024-01-05 reach the database as the same characters. The tool does not guess types from the data.

Decimal (convert) writes the cell as a bare number, such as 12.50 or -3. It accepts an optional sign, digits and one decimal point with digits on both sides. Exponents such as 1e3, spaces around the number, 1., .5 and other text stop the run with an error, so choose Text for that column or clean the cells first.

In CREATE TABLE, Text gives TEXT in SQLite and PostgreSQL and LONGTEXT CHARACTER SET utf8mb4 in MySQL. Decimal gives NUMERIC in SQLite and PostgreSQL, and in MySQL a DECIMAL whose precision and scale cover every value in the column, such as DECIMAL(7,3) for 1234.125. SQLite NUMERIC stores 12.50 as 12.5 and 007 as 7, so keep codes and IDs as Text.

MySQL DECIMAL allows up to 65 digits in total and 30 digits after the decimal point. Values that need more stop the conversion with an error.

Empty cells, NULL and uneven rows

An empty cell becomes an empty string, '', by default. Tick Convert empty cells to NULL to write NULL instead. Exact NULL marker (optional) turns each cell that matches its text into NULL: with the marker NULL, the cell NULL becomes SQL NULL, while null in lower case stays the string 'null'. Both rules also apply to quoted cells, because CSV quotes carry no type.

A row with fewer fields than the widest row gets NULL for each missing field, and the result line counts them as missing cells written as NULL. A row with more fields adds columns named column_4, column_5 and so on, which you can rename. With First row contains headers off, every column gets such a name, and the first row becomes data.

Rows per INSERT sets how many rows share one INSERT statement. The default is 100, so 250 rows give three INSERT statements. Set it to 1 for one statement per row.

How each database gets its SQL

Names are quoted as single identifiers: double quotes in SQLite and PostgreSQL, backticks in MySQL, with any quote inside a name doubled. A table name such as db.table stays one name rather than a schema path, and MySQL rejects a table name with a dot or slash. Column names must differ when case is ignored.

Text values use single quotes with each apostrophe doubled. PostgreSQL writes every value as an E'' string, with backslashes doubled and line breaks as \r and \n. MySQL writes a value that holds a backslash, tab, line break or other control character as CONVERT(X'...' USING utf8mb4), and the file opens with a comment to import over a utf8mb4 connection.

A NUL character stays in SQLite and MySQL output, but PostgreSQL text cannot hold it, so that run stops with an error. PostgreSQL names may use 63 UTF-8 bytes. MySQL names may use 64 characters, with no emoji, and cannot end with a space.

Limits

The tool writes CREATE TABLE and INSERT statements only. It has no SQL Server or Oracle option, and it does not write UPDATE statements, upserts, primary keys, indexes or constraints. It does not connect to a database or run the SQL, so read the output before you import it, and turn off Include CREATE TABLE when the table already exists.

Files must be UTF-8. A byte order mark at the start is removed, and a file in another encoding stops with an error. A quoted field that never closes also stops the run with an error.

The tool sets no cap on rows, columns or file size. A very large file is limited by the memory and string length limits of your browser, and the run can then stop with an error. Cancel stops a slow run and keeps your input and column settings. Reset clears the input and the output and restores the default settings.

Questions

How can I convert a CSV file to SQL?

Paste the CSV or select Choose CSV file, then select Read columns. Choose SQLite, PostgreSQL or MySQL as the Database type, set the Table name and check each column type. Select Download .sql to save converted.sql, or Preview SQL to read the statements first. The file holds a CREATE TABLE and the INSERT statements.

How do I create a SQL table from a CSV file?

Leave Include CREATE TABLE ticked. The output then starts with a CREATE TABLE that has one column for each CSV column, named from the header row. Text columns become TEXT, or LONGTEXT in MySQL, and Decimal columns become NUMERIC, or in MySQL a DECIMAL sized to your values. The INSERT statements follow.

How do I insert CSV data into an existing MySQL table?

Choose MySQL, type the table name and turn off Include CREATE TABLE. Rename each column to match the table, since every INSERT lists the column names. Import the file over a utf8mb4 connection. The tool does not read your table, so it cannot check its keys, types or constraints first.

Can this turn a CSV file into a database?

It writes SQL text, not a database file, and it does not connect to a server. Run the .sql file in your database to create the table and its rows. For SQLite, the sqlite3 shell command .read converted.sql runs the file against the database that you opened.

Does it make SQL for SQL Server or Oracle?

No. The Database type list holds SQLite, PostgreSQL and MySQL only, and each option follows the quoting and text rules of that database. There is no SQL Server or Oracle option, so for those databases use the import tools that come with them.

Why did a Decimal column or a column name give an error?

Decimal takes only digits with an optional sign and decimal point, so 1e3, a number with a space around it, or text such as n/a stops the run. Choose Text for that column. Name errors come from an empty or repeated column name, a PostgreSQL name over 63 bytes, or a MySQL name that ends with a space.

Is my CSV uploaded?

No. The CSV is read and the SQL is written by a worker in your browser, and nothing is uploaded, sent to a database or stored. The download is made on this page. Reset clears the input and the output, but it does not delete a .sql file that you saved or clear your clipboard.