


How to Import a CSV File into a SQLite Database Table Using Python?
Bulk Importing CSV Data into SQLite with Python
Importing a CSV file into a SQLite database table can be a straightforward process using Python's sqlite3 module. However, directly using the ".import" command may not yield desired results, as suggested in a recent query.
To effectively import a CSV file, consider the following approach:
Example:
Assuming you have a CSV file named "data.csv" located in your current working directory and a database connection established using sqlite3.connect(), you can perform the import using the following steps:
import csv, sqlite3 # Connect to the database con = sqlite3.connect(":memory:") # or 'sqlite:///your_filename.db' # Create a cursor cur = con.cursor() # Create the target table in advance cur.execute("CREATE TABLE t (col1, col2);") # Adjust column names as needed # Open the CSV file for reading with open('data.csv','r') as fin: # Create a DictReader to read the header and rows as dictionaries # In case of a headerless file, skip the header argument dr = csv.DictReader(fin, delimiter=',') # Convert the rows into a list of tuples to_db = [(i['col1'], i['col2']) for i in dr] # Use executemany to insert the data efficiently cur.executemany("INSERT INTO t (col1, col2) VALUES (?, ?);", to_db) # Commit the changes con.commit() # Close the connection con.close()
This script assumes your CSV file has two columns named "col1" and "col2." If your file has different column names, adjust them accordingly in the code. Upon successful execution, the CSV data will be imported into the newly created "t" table within the specified database.
The above is the detailed content of How to Import a CSV File into a SQLite Database Table 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 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 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 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...

Fastapi ...

In Python, how to dynamically create an object through a string and call its methods? This is a common programming requirement, especially if it needs to be configured or run...

Using python in Linux terminal...
