from dateutil.parser import parse as date_parse import csv import re import sqlite3 noise = re.compile(r' {3,}.*') def __init_db__(path): con = sqlite3.connect(path) cur = con.cursor() cur.execute(''' CREATE TABLE IF NOT EXISTS `transactions` ( `details` STRING, `date` STRING, `amount` REAL, `type` STRING, `cOs_num` INTEGER, UNIQUE( `details`, `date`, `type`, `amount`, `cOs_num`));''') con.commit() return con class browser: def __init__(self,db_path): self.db_path = db_path self.con = __init_db__(db_path) self.con.row_factory = sqlite3.Row self.cur = self.con.cursor() def update_from_csv(self,csv_file): with open(csv_file) as file: reader = csv.DictReader(file) l = list(reader) for line in l: date = line.pop('Posting Date') cOs = line.pop('Check or Slip #') line['date'] = date_parse(date) line['Description'] = noise.sub('',line['Description']) line['Amount'] = float(line['Amount']) line['cOs'] = cOs self.cur.executemany('INSERT OR IGNORE INTO `transactions` VALUES (:Description,:date,:Amount,:Type,:cOs);',l) self.con.commit() if __name__ == "__main__": b = browser("test.db") b.update_from_csv('test.csv') res = b.cur.execute('SELECT * FROM transactions WHERE amount>=100;')