XhCode Online Converter Tools

HTML Table To SQL Converter

Enter html here:
Results:
HTML Table To SQL Online Converter

Converting an HTML table to SQL typically means taking the data from an HTML table and generating an SQL insert statement that can be used to populate a database.

This is especially useful when you have data in an HTML table format and want to import it into a database without manually entering the data.

Example HTML Table:
html

<table id="data-table">
<tr>
<th>Name</th>
<th>Age</th>
<th>Country</th>
</tr>
<tr>
<td>John</td>
<td>28</td>
<td>USA</td>
</tr>
<tr>
<td>Jane</td>
<td>22</td>
<td>Canada</td>
</tr>
<tr>
<td>Tom</td>
<td>30</td>
<td>UK</td>
</tr>
</table>
Converted SQL Output:
The SQL INSERT statements for inserting the data from the above table would look something like this:

sql

INSERT INTO table_name (Name, Age, Country) VALUES ('John', 28, 'USA');
INSERT INTO table_name (Name, Age, Country) VALUES ('Jane', 22, 'Canada');
INSERT INTO table_name (Name, Age, Country) VALUES ('Tom', 30, 'UK');
How to Convert HTML Table to SQL
1. Using JavaScript (For Web Use)
If you want to convert an HTML table to SQL directly within a webpage using JavaScript, here is an example script that extracts data from the table and creates the corresponding SQL INSERT statements.

JavaScript Code:
html

<!DOCTYPE html>
<html>
<head>
<title>HTML Table to SQL</title>
</head>
<body>
<table id="data-table">
<tr>
<th>Name</th>
<th>Age</th>
<th>Country</th>
</tr>
<tr>
<td>John</td>
<td>28</td>
<td>USA</td>
</tr>
<tr>
<td>Jane</td>
<td>22</td>
<td>Canada</td>
</tr>
<tr>
<td>Tom</td>
<td>30</td>
<td>UK</td>
</tr>
</table>
<button onclick="generateSQL()">Generate SQL</button>

<pre id="sql-output"></pre>

<script>
function generateSQL() {
var table = document.getElementById("data-table");
var rows = table.rows;
var sqlStatements = [];

// Get the header row (column names)
var headers = [];
for (var i = 0; i < rows[0].cells.length; i++) {
headers.push(rows[0].cells[i].textContent);
}

// Loop through each row (skip the first header row)
for (var i = 1; i < rows.length; i++) {
var row = rows[i];
var values = [];
for (var j = 0; j < row.cells.length; j++) {
var value = row.cells[j].textContent;
values.push(`'${value}'`); // Add quotes around text values
}

// Create the INSERT statement
var sql = `INSERT INTO table_name (${headers.join(', ')}) VALUES (${values.join(', ')});`;
sqlStatements.push(sql);
}

// Output the SQL statements
document.getElementById("sql-output").textContent = sqlStatements.join("\n");
}
</script>
</body>
</html>
Explanation:
The function generateSQL() grabs the data from the HTML table and generates INSERT INTO SQL statements for each row in the table.
The first row (header) is used to determine the column names.
Each subsequent row is used to extract values and create the corresponding SQL statement.
The generated SQL statements are displayed in a <pre> tag, which makes the output formatted and readable.
Steps:
Copy the code into an HTML file.
Open the file in your browser.
Click the "Generate SQL" button to generate the SQL insert statements.
2. Using Python (For Automated or Bulk Conversion)
If you have multiple HTML files or want to automate the conversion of HTML tables to SQL, you can use Python with BeautifulSoup to extract the data and generate SQL INSERT statements.

Python Script:
python

from bs4 import BeautifulSoup

def html_table_to_sql(html_file, table_name):
with open(html_file, 'r') as file:
soup = BeautifulSoup(file, 'html.parser')
table = soup.find('table')

# Get the header row (column names)
headers = [header.text.strip() for header in table.find_all('th')]

sql_statements = []

# Loop through the rows and extract the cell data
for row in table.find_all('tr')[1:]: # Skip the first row (header)
cells = row.find_all('td')
if len(cells) > 0:
values = [cell.text.strip() for cell in cells]
sql = f"INSERT INTO {table_name} ({', '.join(headers)}) VALUES ({', '.join([f'\'{v}\'' for v in values])});"
sql_statements.append(sql)

# Output the SQL statements
return "\n".join(sql_statements)

# Example usage
html_file = 'your_file.html'
table_name = 'your_table_name'
sql_output = html_table_to_sql(html_file, table_name)
print(sql_output)
Explanation:
BeautifulSoup is used to parse the HTML and extract the table rows and headers.
The script constructs INSERT INTO SQL statements for each row of the table, with values properly enclosed in quotes.
It returns a string containing all the SQL statements.
Steps:
Install the required libraries: pip install beautifulsoup4 lxml.
Run the script with the path to your HTML file and the name of the target table.
The script will output the SQL insert statements to the console or file.
3. Using Online Tools:
You can use online tools to quickly convert HTML tables into SQL statements.

TableConvert:

Paste the HTML code or upload your HTML file.
The tool will automatically generate SQL INSERT statements for your table.
You can download the SQL file or copy the SQL code to your clipboard.
ConvertCSV:

This tool can convert HTML tables into SQL INSERT statements, allowing you to paste the table data or upload the HTML file.
4. Manually Writing SQL Statements:
If you have a small table, you can manually convert the HTML table into SQL statements. For example:

html

<table>
<tr>
<th>Name</th>
<th>Age</th>
<th>Country</th>
</tr>
<tr>
<td>John</td>
<td>28</td>
<td>USA</td>
</tr>
<tr>
<td>Jane</td>
<td>22</td>
<td>Canada</td>
</tr>
</table>
The manually written SQL insert statements would be:

sql

INSERT INTO table_name (Name, Age, Country) VALUES ('John', 28, 'USA');
INSERT INTO table_name (Name, Age, Country) VALUES ('Jane', 22, 'Canada');
Summary of Methods:
JavaScript (Browser Solution): Use JavaScript to extract the table data and generate SQL INSERT statements directly in the browser.
Python (Automated Solution): Use Python and BeautifulSoup to automate the conversion of HTML tables to SQL, ideal for handling multiple tables or files.
Online Tools: Websites like TableConvert and ConvertCSV allow you to convert HTML tables to SQL with minimal effort.
Manual Writing: For small tables, you can manually write the SQL INSERT statements.