


How can I import CSV files into SQLite databases using Python?
Importing CSV Files into SQLite Databases with Python
In Python, utilizing the sqlite3 module empowers developers to effortlessly import data from CSV files into sqlite3 database tables. While the ".import" command might not be directly applicable, alternative methods provide a straightforward approach to accomplish this task.
Example Code:
To illustrate the import process, consider the following Python code:
import csv, sqlite3 # Connect to the database (in-memory or file) and create a cursor con = sqlite3.connect(":memory:") # change to 'sqlite:///your_filename.db' cur = con.cursor() cur.execute("CREATE TABLE t (col1, col2);") # use your column names here # Open the CSV file for reading with open('data.csv','r') as fin: # Create a DictReader object to read data from the CSV file dr = csv.DictReader(fin) # comma is default delimiter # Convert CSV data into a list of tuples for database insertion to_db = [(i['col1'], i['col2']) for i in dr] # Execute the insert query using executemany to efficiently import data cur.executemany("INSERT INTO t (col1, col2) VALUES (?, ?);", to_db) # Commit changes to the database con.commit() # Close the connection and cursor con.close()
Explanation:
- Connect to the SQLite database using sqlite3.connect() and create a cursor for executing queries.
- Create the target table in the database using the CREATE TABLE statement.
- Open the CSV file for reading using the with statement.
- Use csv.DictReader to read data from the CSV file. It automatically maps column names to their respective values.
- Convert the data into a list of tuples for efficient insertion into the database.
- Utilize executemany to import multiple rows simultaneously.
- Commit the changes to make the data persistent in the database.
- Close the connection and cursor to release resources.
The above is the detailed content of How can I import CSV files into SQLite databases using Python?. For more information, please follow other related articles on the PHP Chinese website!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

Solution to permission issues when viewing Python version in Linux terminal When you try to view Python version in Linux terminal, enter python...

How to avoid being detected when using FiddlerEverywhere for man-in-the-middle readings When you use FiddlerEverywhere...

When using Python's pandas library, how to copy whole columns between two DataFrames with different structures is a common problem. Suppose we have two Dats...

How to teach computer novice programming basics within 10 hours? If you only have 10 hours to teach computer novice some programming knowledge, what would you choose to teach...

How does Uvicorn continuously listen for HTTP requests? Uvicorn is a lightweight web server based on ASGI. One of its core functions is to listen for HTTP requests and proceed...

Using python in Linux terminal...

Fastapi ...

Understanding the anti-crawling strategy of Investing.com Many people often try to crawl news data from Investing.com (https://cn.investing.com/news/latest-news)...
