Bulk Import

Date: March 11th 2016
Last updated: March 11th 2016

Going from csv to an sqlite table is useful. Perhaps more useful will be to separate the data from a csv file into different tables connected with a foriegn key. For now, this is a simple table to table upload.

data

# data to upload

# score,name #<=== header
# 12.1,Ray
# 13.2,Jess
# 89,John
# 96.3,James
# 78.2,Jason
# 17.5,Jake
# 13.3,Jacob

code

import csv, sqlite3

# connect
con = sqlite3.connect("gitbook.db")

# cursor
cur = con.cursor()

# read data
with open('data.csv','r') as fin:
    dr = csv.DictReader(fin)
    # create list of tuples
    to_db = [(i['score'], i['name']) for i in dr]

# execute
cur.executemany("INSERT INTO scoreboard (score, name) \
                 VALUES (?, ?);", to_db)

# commit
con.commit()

# close
con.close()

GUI
sqliteman screenshot showing the result of bulk import script from csv

results matching ""

    No results matching ""