Load data from Excel to SQLite database using Python

Here is a sample Python code to write data to SQLite database from an Excel file using the pandas library

import pandas as pd
import sqlite3

# Connect to the SQLite database
conn = sqlite3.connect('example.db')

# Load the Excel file into a pandas dataframe
df = pd.read_excel('example.xlsx')

# Write the data to the SQLite database table
df.to_sql(name='example_table', con=conn, if_exists='replace', index=False)

# Close the connection to the SQLite database
conn.close()

In this example, we first import the pandas and sqlite3 libraries. We then establish a connection to a SQLite database using the connect() method of the sqlite3 module.

Next, we use the read_excel() method of the pandas library to load the data from an Excel file into a pandas dataframe. We then use the to_sql() method of the pandas dataframe to write the data to a new table in the SQLite database. The if_exists parameter is set to ‘replace’, which will drop the table if it already exists and create a new one with the same name. The index parameter is set to False to prevent the index column from being written to the database.

Finally, we close the connection to the SQLite database using the close() method of the sqlite3 module.

Pamai Tech
Turning ideas into Reality

Products

Office Add-in

Enterprise Solutions

Cloud Consulting

UI UX Design

Data Transformation

Services

FAQ's

Privacy Policy

Terms & Condition

Team

Contact Us

Company

About Us

Services

Features

Our Pricing

Latest News

© 2023 Pamai Tech