Convert CSV to SQL
Max file size 100mb.
CSV vs SQL Format Comparison
| Aspect | CSV (Source Format) | SQL (Target Format) |
|---|---|---|
| Format Overview |
CSV
Comma-Separated Values
Plain text format for storing tabular data where each line represents a row and values are separated by commas (or other delimiters). Universally supported by spreadsheets, databases, and data processing tools. Simple, compact, and human-readable. Tabular Data Universal |
SQL
Structured Query Language
Standard language for relational database management. SQL statements define table schemas (CREATE TABLE) and insert data (INSERT INTO). The output is a portable script that can be executed on MySQL, PostgreSQL, SQLite, SQL Server, or any SQL-compliant database. Database Structured Data |
| Technical Specifications |
Structure: Rows and columns in plain text
Delimiter: Comma, semicolon, tab, or pipe Encoding: UTF-8, ASCII, or UTF-8 with BOM Headers: Optional first row as column names Extensions: .csv |
Structure: DDL and DML statements
Standard: ANSI SQL (ISO/IEC 9075) Encoding: UTF-8 Statements: CREATE TABLE, INSERT INTO Extensions: .sql |
| Syntax Examples |
CSV uses delimiter-separated values: Name,Age,City Alice,30,New York Bob,25,London Charlie,35,Tokyo |
SQL uses DDL/DML statements: CREATE TABLE data (
Name TEXT,
Age TEXT,
City TEXT
);
INSERT INTO data (Name, Age, City)
VALUES ('Alice', '30', 'New York');
INSERT INTO data (Name, Age, City)
VALUES ('Bob', '25', 'London');
INSERT INTO data (Name, Age, City)
VALUES ('Charlie', '35', 'Tokyo');
|
| Content Support |
|
|
| Advantages |
|
|
| Disadvantages |
|
|
| Common Uses |
|
|
| Best For |
|
|
| Version History |
Introduced: 1972 (early implementations)
RFC Standard: RFC 4180 (2005) Status: Widely used, stable MIME Type: text/csv |
Introduced: 1974 (IBM SEQUEL)
Current Standard: ISO/IEC 9075:2023 Status: Active development, evolving MIME Type: application/sql |
| Software Support |
Microsoft Excel: Full support
Google Sheets: Full support LibreOffice Calc: Full support Other: Python, R, pandas, SQL, all databases |
MySQL/MariaDB: Full support
PostgreSQL: Full support SQLite: Full support Other: SQL Server, Oracle, DBeaver, pgAdmin |
Why Convert CSV to SQL?
Converting CSV data to SQL statements is one of the most common data engineering tasks. It transforms flat tabular data from spreadsheets into database-ready scripts containing CREATE TABLE definitions and INSERT INTO statements. Instead of manually writing SQL to import data, you can export from Excel or Google Sheets as CSV and instantly generate a SQL script that creates the table and populates it with all your data.
The converter analyzes your CSV structure to generate appropriate column names from headers and creates INSERT statements for each row. Our tool generates ANSI-compatible SQL that works with MySQL, PostgreSQL, SQLite, SQL Server, and other relational databases. Values are properly escaped to prevent SQL injection issues, and string values are correctly quoted.
This conversion is invaluable for developers who need to seed databases with test data, migrate data from spreadsheets into production databases, or create database fixtures for automated testing. Rather than using database-specific bulk import tools, a SQL script provides a portable, human-readable, and version-controllable way to load data that works across different database systems.
CSV to SQL conversion is also essential for data analysts who need to load CSV datasets into a database for querying with SQL. By converting CSV to SQL INSERT statements, you can populate a database table and then run complex joins, aggregations, and filters that are impossible to perform on raw CSV files. The SQL output can be executed directly in any database client or command-line tool.
Key Benefits of Converting CSV to SQL:
- Database Ready: Complete CREATE TABLE and INSERT INTO statements ready to execute
- Auto-Detection: Automatically detects CSV delimiter (comma, semicolon, tab, pipe)
- Header to Columns: CSV headers become SQL column names automatically
- Proper Escaping: String values are escaped to prevent SQL injection
- Cross-Database: ANSI SQL compatible with MySQL, PostgreSQL, SQLite, and more
- Batch Inserts: Efficient INSERT statements for fast data loading
- Data Integrity: All cell values are preserved exactly as in the original CSV
- Version Control: SQL scripts are plain text, perfect for Git repositories
Practical Examples
Example 1: Product Catalog Import
Input CSV file (products.csv):
product_id,name,price,category 1,Wireless Mouse,29.99,Electronics 2,USB Keyboard,49.99,Electronics 3,Monitor Stand,39.99,Accessories 4,Webcam HD,59.99,Electronics
Output SQL file (products.sql):
CREATE TABLE products (
product_id TEXT,
name TEXT,
price TEXT,
category TEXT
);
INSERT INTO products (product_id, name, price, category)
VALUES ('1', 'Wireless Mouse', '29.99', 'Electronics');
INSERT INTO products (product_id, name, price, category)
VALUES ('2', 'USB Keyboard', '49.99', 'Electronics');
INSERT INTO products (product_id, name, price, category)
VALUES ('3', 'Monitor Stand', '39.99', 'Accessories');
INSERT INTO products (product_id, name, price, category)
VALUES ('4', 'Webcam HD', '59.99', 'Electronics');
Example 2: User Accounts Migration
Input CSV file (users.csv):
username,email,role,created_date admin,[email protected],administrator,2024-01-15 jsmith,[email protected],editor,2024-02-20 mjones,[email protected],viewer,2024-03-10
Output SQL file (users.sql):
CREATE TABLE users (
username TEXT,
email TEXT,
role TEXT,
created_date TEXT
);
INSERT INTO users (username, email, role, created_date)
VALUES ('admin', '[email protected]', 'administrator', '2024-01-15');
INSERT INTO users (username, email, role, created_date)
VALUES ('jsmith', '[email protected]', 'editor', '2024-02-20');
INSERT INTO users (username, email, role, created_date)
VALUES ('mjones', '[email protected]', 'viewer', '2024-03-10');
Example 3: Log Entries for Analysis
Input CSV file (logs.csv):
timestamp,level,source,message 2024-01-15 10:30:00,ERROR,auth,Login failed for user 'admin' 2024-01-15 10:31:00,INFO,api,GET /api/users returned 200 2024-01-15 10:32:00,WARN,db,Slow query detected (2.3s)
Output SQL file (logs.sql):
CREATE TABLE logs (
timestamp TEXT,
level TEXT,
source TEXT,
message TEXT
);
INSERT INTO logs (timestamp, level, source, message)
VALUES ('2024-01-15 10:30:00', 'ERROR', 'auth', 'Login failed for user ''admin''');
INSERT INTO logs (timestamp, level, source, message)
VALUES ('2024-01-15 10:31:00', 'INFO', 'api', 'GET /api/users returned 200');
INSERT INTO logs (timestamp, level, source, message)
VALUES ('2024-01-15 10:32:00', 'WARN', 'db', 'Slow query detected (2.3s)');
Frequently Asked Questions (FAQ)
Q: What SQL statements does the converter generate?
A: The converter generates two types of SQL statements: a CREATE TABLE statement that defines the table schema with column names derived from CSV headers, and INSERT INTO statements for each row of data. The table name is derived from the CSV filename. All columns are defined as TEXT type for maximum compatibility. You can modify data types in the output to match your database schema needs.
Q: How does the CSV delimiter detection work?
A: Our converter uses Python's csv.Sniffer to automatically detect the delimiter used in your CSV file. It supports commas, semicolons, tabs, and pipe characters. The sniffer analyzes a sample of your file to determine the correct delimiter and quoting style. CSV files from Excel, Google Sheets, European locale software, or database exports are all handled correctly without any manual configuration.
Q: Which databases are compatible with the generated SQL?
A: The generated SQL uses ANSI-standard syntax compatible with MySQL, MariaDB, PostgreSQL, SQLite, Microsoft SQL Server, Oracle, and other SQL-compliant databases. The CREATE TABLE and INSERT statements use standard TEXT data types and quoted string values that work across all major database systems.
Q: How are CSV headers mapped to SQL column names?
A: The first row of the CSV file is used as column names in the CREATE TABLE statement. Column names are preserved as-is from the CSV headers. If your CSV has no header row, generic column names (column1, column2, etc.) are generated. Spaces and special characters in header names are preserved, though you may want to sanitize them for your specific database.
Q: How are special characters and quotes handled in SQL values?
A: Single quotes within data values are escaped by doubling them (e.g., O'Brien becomes O''Brien), which is the standard SQL escaping convention. This prevents SQL syntax errors and SQL injection vulnerabilities. Other special characters are preserved as-is within the quoted string values.
Q: What about data types -- are numbers and dates detected?
A: The converter uses TEXT type for all columns to ensure maximum compatibility and data preservation. CSV files do not contain type information, so using TEXT prevents data loss or conversion errors. After generating the SQL script, you can manually change column types to INTEGER, DECIMAL, DATE, or other types as needed for your specific database schema.
Q: Can I import the SQL file directly into MySQL or PostgreSQL?
A: Yes! The generated SQL file can be executed directly using command-line tools like mysql -u user -p database < file.sql for MySQL or psql -U user -d database -f file.sql for PostgreSQL. You can also open the file in database management tools like DBeaver, phpMyAdmin, pgAdmin, or MySQL Workbench and execute the statements.
Q: Is there a limit on the number of rows that can be converted?
A: There is no hard limit on the number of rows. However, very large CSV files will produce correspondingly large SQL files since each row generates a separate INSERT statement. For datasets with millions of rows, consider using database-specific bulk import tools (LOAD DATA INFILE for MySQL, COPY for PostgreSQL) which are more efficient than individual INSERT statements.
Q: Does the converter support CSV files from Excel?
A: Yes! CSV files exported from Microsoft Excel, Google Sheets, LibreOffice Calc, and other spreadsheet applications are fully supported. The converter handles both UTF-8 and UTF-8 with BOM encodings, as well as different line ending styles (Windows CRLF, Unix LF, Mac CR). Excel's default comma-separated format and locale-specific semicolon-separated formats are both detected automatically.