How to Import a CSV file into a SQLite database Table using Python? Last Updated : 28 Oct, 2021 Comments Improve Suggest changes Like Article Like Report In this article, we are going to discuss how to import a CSV file content into an SQLite database table using Python. Approach:At first, we import csv module (to work with csv file) and sqlite3 module (to populate the database table).Then we connect to our geeks database using the sqlite3.connect() method.At this point, we create a cursor object to handle queries on the database table.We first create our person table and create a csv file with the contents inside which we will be inserting into our table.We open the above-created csv file using the open() function.We extract all the contents of the csv file into our contents variable through csv.reader() method.Then we insert our row-wise contents of csv file into our database through executemany() method which will replace (?,?) with the next two comma-separated data of the csv file and insert it as a record into the person table.Finally, we verify that the data of the csv file has been successfully inserted into our table with the SELECT statement and commit the changes and close the database connection.Below is the implementation. For the purpose of implementation, we will be creating a person table in our geeks.db database. We are going to insert the content of the person_records.csv in our person table. Below is the CSV file we are going to use: Below is the complete program based on the above approach: Python3 # Import required modules import csv import sqlite3 # Connecting to the geeks database connection = sqlite3.connect('g4g.db') # Creating a cursor object to execute # SQL queries on a database table cursor = connection.cursor() # Table Definition create_table = '''CREATE TABLE person( id INTEGER PRIMARY KEY AUTOINCREMENT, name TEXT NOT NULL, age INTEGER NOT NULL); ''' # Creating the table into our # database cursor.execute(create_table) # Opening the person-records.csv file file = open('person-records.csv') # Reading the contents of the # person-records.csv file contents = csv.reader(file) # SQL query to insert data into the # person table insert_records = "INSERT INTO person (name, age) VALUES(?, ?)" # Importing the contents of the file # into our person table cursor.executemany(insert_records, contents) # SQL query to retrieve all data from # the person table To verify that the # data of the csv file has been successfully # inserted into the table select_all = "SELECT * FROM person" rows = cursor.execute(select_all).fetchall() # Output to the console screen for r in rows: print(r) # Committing the changes connection.commit() # closing the database connection connection.close() Output: SQLite: Comment More info R remmargorpp Follow Improve Article Tags : Python Python-SQLite Explore Python FundamentalsPython Introduction 3 min read Input and Output in Python 4 min read Python Variables 5 min read Python Operators 5 min read Python Keywords 2 min read Python Data Types 7 min read Conditional Statements in Python 3 min read Loops in Python - For, While and Nested Loops 6 min read Python Functions 5 min read Recursion in Python 4 min read Python Lambda Functions 5 min read Python Data StructuresPython String 5 min read Python Lists 4 min read Python Tuples 4 min read Python Dictionary 3 min read Python Sets 6 min read Python Arrays 7 min read List Comprehension in Python 4 min read Advanced PythonPython OOP Concepts 11 min read Python Exception Handling 5 min read File Handling in Python 4 min read Python Database Tutorial 4 min read Python MongoDB Tutorial 2 min read Python MySQL 9 min read Python Packages 12 min read Python Modules 7 min read Python DSA Libraries 15 min read List of Python GUI Library and Packages 3 min read Data Science with PythonNumPy Tutorial - Python Library 3 min read Pandas Tutorial 6 min read Matplotlib Tutorial 5 min read Python Seaborn Tutorial 15+ min read StatsModel Library- Tutorial 4 min read Learning Model Building in Scikit-learn 8 min read TensorFlow Tutorial 2 min read PyTorch Tutorial 6 min read Web Development with PythonFlask Tutorial 8 min read Django Tutorial | Learn Django Framework 7 min read Django ORM - Inserting, Updating & Deleting Data 4 min read Templating With Jinja2 in Flask 6 min read Django Templates 7 min read Python | Build a REST API using Flask 3 min read How to Create a basic API using Django Rest Framework ? 4 min read Python PracticePython Quiz 3 min read Python Coding Practice 1 min read Python Interview Questions and Answers 15+ min read Like