Creating a sqlite database from CSV with Python Last Updated : 26 Dec, 2020 Summarize Comments Improve Suggest changes Share Like Article Like Report Prerequisites: PandasSQLite SQLite is a software library that implements a lightweight relational database management system. It does not require a server to operate unlike other RDBMS such as PostgreSQL, MySQL, Oracle, etc. and applications directly interact with a SQLite database. SQLite is often used for small applications, particularly in embedded systems and mobile applications. To interact with a SQLite database in Python, the sqlite3 module is required. ApproachImport moduleCreate a database and establish connection- To establish a connection, we use the sqlite3.connect() function which returns a connection object. Pass the name of the database to be created inside this function. The complete state of a SQLite database is stored in a file with .db extension. If the path is not specified then the new database is created in the current working directory. Syntax: sqlite3.connect('database_name.db') Import csv using read_csv() Syntax: pandas.read_csv('file_name.csv') Write the contents to a new table- The function to_sql() creates a new table from records of the dataframe. Pass the table name and connection object inside this function. The column names of the table are same as the header of the CSV file. By default, the dataframe index is written as a column. Simply toggle the index parameter to False in order to remove this column. Additionally, the if_exists parameter specifies the behavior in case the table name is already being used. It can either raise error (fail), append new values or replace the existing table. pandas.DataFrame.to_sql(table_name, connection_object, if_exists, index) Check the table contents- Create a cursor object and execute the standard SELECT statement to fetch the contents of the newly created table. Close connection Csv file in use: stud_data.csv Program: Python3 # Import required libraries import sqlite3 import pandas as pd # Connect to SQLite database conn = sqlite3.connect(r'C:\User\SQLite\University.db') # Load CSV data into Pandas DataFrame stud_data = pd.read_csv('stud_data.csv') # Write the data to a sqlite table stud_data.to_sql('student', conn, if_exists='replace', index=False) # Create a cursor object cur = conn.cursor() # Fetch and display result for row in cur.execute('SELECT * FROM student'): print(row) # Close connection to SQLite database conn.close() Output: Comment More infoAdvertise with us Next Article Interface Python with an SQL Database A akshisaxena Follow Improve Article Tags : Python Python-database python-csv Practice Tags : python Similar Reads Python SQLite - Creating a New Database In this article, we will discuss how to create a Database in SQLite using Python. Creating a Database You do not need any special permissions to create a database. The sqlite3 command used to create the database has the following basic syntax Syntax: $ sqlite3 <database_name_with_db_extension> 3 min read Python SQLite - Connecting to Database In this article, we'll discuss how to connect to an SQLite database in Python using the sqlite3 module, perform basic operations, and handle errors effectively.Connecting to the DatabaseTo interact with an SQLite database, we first need to establish a connection to it using the connect() method. If 2 min read Interface Python with an SQL Database Python is an easy-to-learn language and connectivity of python with any SQL database is a much-desired option to have the persistence feature. Python is an object-oriented programming language and it is open source. Newcomers to the software industry including school children too can learn Python ea 8 min read How to Import a CSV file into a SQLite database Table using Python? 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() 3 min read Connecting Pandas to a Database with SQLAlchemy In this article, we will discuss how to connect pandas to a database and perform database operations using SQLAlchemy. The first step is to establish a connection with your existing database, using the create_engine() function of SQLAlchemy. Syntax: from sqlalchemy import create_engine engine = crea 3 min read Python SQLite - Working with Date and DateTime SQLite does not support built-in DateTime storage a class, but SQLite allows us to work with timestamp types. We can store and retrieve Python date and datetime information stored in the SQLite database tables by converting them to Python date and datetime types and vice-versa. While inserting the d 4 min read Like