From f9adcaf8f55be3c89ef6bb7207ce71b342d09ac5 Mon Sep 17 00:00:00 2001 From: rlbr Date: Sun, 16 Dec 2018 16:08:05 -0600 Subject: [PATCH] taking balance into account but not cOs; added datetime converter/adapter --- db_wrapper_class.py | 28 ++++++++++++++-------------- 1 file changed, 14 insertions(+), 14 deletions(-) diff --git a/db_wrapper_class.py b/db_wrapper_class.py index 4189ec2..35c121e 100644 --- a/db_wrapper_class.py +++ b/db_wrapper_class.py @@ -1,24 +1,28 @@ from dateutil.parser import parse as date_parse import csv +import datetime import re import sqlite3 -noise = re.compile(r' {3,}.*') +#noise = re.compile(r' {3,}.*') +sqlite3.register_adapter(datetime.datetime,lambda dt: str(dt)) +sqlite3.register_converter("DATE",date_parse) def __init_db__(path): - con = sqlite3.connect(path) + con = sqlite3.connect(path,detect_types=sqlite3.PARSE_DECLTYPES) cur = con.cursor() cur.execute(''' CREATE TABLE IF NOT EXISTS `transactions` ( `details` STRING, - `date` STRING, + `posting_date` DATE, `amount` REAL, + `balance` REAL, `type` STRING, - `cOs_num` INTEGER, - UNIQUE( + UNIQUE ( `details`, - `date`, - `type`, + `posting_date`, `amount`, - `cOs_num`));''') + `balance`, + `type`) + )''') con.commit() return con @@ -36,14 +40,10 @@ class browser: 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['posting_date'] = date_parse(date) line['Amount'] = float(line['Amount']) - line['cOs'] = cOs - self.cur.executemany('INSERT OR IGNORE INTO `transactions` VALUES (:Description,:date,:Amount,:Type,:cOs);',l) + self.cur.executemany('INSERT OR IGNORE INTO `transactions` VALUES (:Description,:posting_date,:Amount,:Balance,:Type);',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;') \ No newline at end of file