2011/06/08

import CSV file into sqlitedb from python

Here I show you how to operate sqlitedb via python.

import sqlite3

def line_generator(input_path):
    _file = open(input_path, "r")
    for _line in _file:
        yield _line.strip().split(',')

def main():
    con = connect_db()
    create_table(con)
    import_table(con)
    export_table(con)
    select_table(con)
    disconnect_db(con)

def connect_db():
    con = sqlite3.connect(":memory:")
    return con

def disconnect_db(con):
    con.close()

def create_table(con):
    con.execute("create table test(id int, name text);")

def drop_table(con):
    con.execute("create table test(id int, name text);")

def import_table(con):
    iterator = line_generator("/home/yaboo/test1.csv")
    for i in iterator:
        con.execute("insert into test values (?, ?)", i)

def export_table(con):
    f = open("/home/yaboo/output_test1.csv", "w")
    writer = csv.writer(f)
    for row in con.execute("SELECT * FROM test;"):
        writer.writerow([col.encode('utf-8') if isinstance(col, unicode) else col for col in row])

def select_table(con)
    cur = con.cursor()
    cur.execute("select * from test;")
    for line in cur: print line

if __name__ == "__main__":
    main() 

No comments:

Post a Comment

100