|
|
|
@ -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;') |