Skip to content

WebNinjaDeveloper.com

Programming Tutorials




Menu
  • Home
  • Youtube Channel
  • Official Blog
  • Nearby Places Finder
  • Direction Route Finder
  • Distance & Time Calculator
Menu

Python 3 Pandas Script to Add Data inside SQLite Database Using CSV File

Posted on January 7, 2023

 

 

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

 

 

Python
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

 

 

 

Recent Posts

  • React.js Twitter API Tutorial to Embed Profile & Timeline, Hashtags of User in Browser Using Javascript
  • Android Java Tutorial to Change Styles & Visibility of System Bars (Top, Action & Status) Full Example
  • Android Java Project to Display & Play Audio Files From Storage inside ListView Using MediaPlayer Class
  • Android Java Project to Build MP4 Video to MP3 Audio Converter Using MediaMuxer Class
  • Android Java Project to Merge Multiple PDF Documents From Gallery Using iTextPDF Library
  • Angular
  • Bunjs
  • C#
  • Deno
  • django
  • Electronjs
  • java
  • javascript
  • Koajs
  • kotlin
  • Laravel
  • meteorjs
  • Nestjs
  • Nextjs
  • Nodejs
  • PHP
  • Python
  • React
  • ReactNative
  • Svelte
  • Tutorials
  • Vuejs




©2023 WebNinjaDeveloper.com | Design: Newspaperly WordPress Theme