How to import CSV file in SQLite database using Python ?
In this article, we’ll learn how to import data from a CSV file and store it in a table in the SQLite database using Python. You can download the CSV file from here which contains sample data on the name and age of a few students.
- Importing necessary modules
- Read data from CSV file DictReader()
- Establish a connection with the database.
sqliteConnection = sqlite3.connect('sql.db') cursor = sqliteConnection.cursor()
- Create the student table and execute the query using execute() method.
- Inserting data into the table
cursor.executemany("insert into student (name, age) VALUES (?, ?);", student_info)
- Read data from the table
- And close the database.
Below is the implementation: