Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to import excel data into sqlite database in python?

Tags:

python-3.x

I have data in multiple excel files, I have to import excel specific sheet data into an SQLite database using python.

I prepared these excel files every week so once the table is created into SQLite database and one week file is imported then every week I have to append excel file.

like image 293
Sandeep Bhatt Avatar asked Sep 04 '26 02:09

Sandeep Bhatt


2 Answers

I have simply written below code into the juypitor notebook and it imports my excel data into sqlite3 database. working fine.

import sqlite3
import pandas as pd

con = sqlite3.connect('cps.db')
wb = pd.read_excel('CPS\CPS.xlsx',sheet_name = None)

for sheet in wb:
    wb[sheet].to_sql(sheet,con,index=False)
con.commit()
con.close()
like image 69
Sandeep Bhatt Avatar answered Sep 06 '26 18:09

Sandeep Bhatt


You can try openpyxl to read Excel(.xlsx) files. Then you can use Sqlite3 module to write the data into the sqlite3 database.

openpyxl is a third party module, so you have to use pip to install it.

pip install openpyxl

sqlite3 module is built in the default python modules, so you don't need to pip for it.

There are tons of examples on how to use openxl on its web page. You'll find the way how to read the data in your Excel files.

like image 41
Takashi Avatar answered Sep 06 '26 18:09

Takashi



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!