SQLite transaction for CSV importing

前端 未结 3 1607
眼角桃花
眼角桃花 2020-12-19 21:56

I\'m very new to python and have been working on my raspberry pi to get a script up and running to import millions of sensor data records into sqlite. I want to do this in t

3条回答
  •  误落风尘
    2020-12-19 22:46

    There were two problems in the code snippet in the quest:

    1. the reader in the call to chunks should have been wrapped in list()
    2. the 'commit' should have been using the connection's commit() method

    See the fixed code:

    import csv, sqlite3, time
    
    def chunks(data, rows=10000):
        for i in range (0, len(data), rows):
                yield data[i:i+rows]
    
    if __name__ == "__main__":
    
        t = time.time()
    
    con = sqlite3.connect('test.db')
    cur = con.cursor()
    cur.execute("DROP TABLE IF EXISTS sensor;")
    cur.execute("CREATE TABLE sensor(key INT, reading REAL);")
    
    filename = 'dummy.csv'
    reader = csv.reader(open(filename,"r"))
    divdata = chunks(list(reader))
    
    for chunk in divdata:
        cur.execute('BEGIN TRANSACTION')
    
        for col1, col2 in chunk:
                cur.execute('INSERT INTO sensor (key, reading) VALUES (?, ?)', (col1, col2))
    
        con.commit()
    

提交回复
热议问题