Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to import big CSV file into Sqlite3 in python?

Tags:

python

sqlite

csv

I'm having a really big CSV file that I need to load into a table in sqlite3. I can't load whole CSV content as a variable into RAM because data is so big, that event with defining types for each column it cannot fit into 64 GB of RAM.

I've tried to use numpy and pandas to load and convert data, but still jumping way above RAM limit.

I would like to somehow read CSV 1 row at a time (or in smaller batches) and progressively save them into the database to keep RAM usage low. Would be perfect if it could be done using more than one CPU core.

like image 464
Tomasz Dylewski Avatar asked Sep 08 '26 07:09

Tomasz Dylewski


1 Answers

I've found a solution digging myself and combining answers from other Stack Overflow questions. Code should be like this:

import sqlite3
import pandas as pd

def add_to_db(row, con):
    # Function that make insert to your DB, make your own.

def process_chunk(chunk):
    # Handles one chunk of rows from pandas reader.
    con = sqlite3.connect("favorita.db")
    for row in chunk:
        add_to_db(row, con)
    con.commit()

for chunk in pd.read_csv('data.csv', chunksize=100000):
    # Adjust chunksize to your needs and RAM size.
    process_chunk(chunk.values)

This could surely be further adjusted to use multi-threading, but I couldn't do it due to deadlocks in database when doing inserts in parallel. But if you have time, this is a solid solution.

like image 153
Tomasz Dylewski Avatar answered Sep 10 '26 22:09

Tomasz Dylewski



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!