Welcome folks today in this blog post we will be adding the data inside the sqllite
database using the pandas
library in command line. All the full source code of the application is shown below.
Get Started
In order to get started you need to install the below libraries using the pip
command as shown below
pip install pandas
pip install sqllite3
And now you need to make an app.py
file and copy paste the following code
app.py
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
import pandas as pd import sqlite3 # Read the CSV file df = pd.read_csv('output.csv') # Connect to the database conn = sqlite3.connect('database.db') # Convert the CSV file to a table in the database df.to_sql('table_name', conn, if_exists='replace') # Close the connection conn.close() |
As you can see we are importing the pandas
and sqlite3
libraries at the top and then we are reading the contents of the csv
file using the read_csv()
method and then we are connecting to the sqlite
database using connect()
method passing the database name that we need to create. And then we are using the to_sql()
method to insert the information from the csv
file to the sqllite
table and lastly we are closing the database connection.
Now we if you execute the python
script you will see the database
file will be created holding the information from the csv files inside the tables
python app.py