Bulk insert huge data into SQLite using Python

前端 未结 3 818
你的背包
你的背包 2020-12-01 08:23

I read this: Importing a CSV file into a sqlite3 database table using Python

and it seems that everyone suggests using line-by-line reading instead of using bulk .im

3条回答
  •  甜味超标
    2020-12-01 08:39

    Since this is the top result on a Google search I thought it might be nice to update this question.

    From the python sqlite docs you can use

    import sqlite3
    
    persons = [
        ("Hugo", "Boss"),
        ("Calvin", "Klein")
    ]
    
    con = sqlite3.connect(":memory:")
    
    # Create the table
    con.execute("create table person(firstname, lastname)")
    
    # Fill the table
    con.executemany("insert into person(firstname, lastname) values (?,?)", persons)
    

    I have used this method to commit over 50k row inserts at a time and it's lightning fast.

提交回复
热议问题