You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

48 lines
1.5 KiB

7 years ago
  1. from dateutil.parser import parse as date_parse
  2. import csv
  3. import re
  4. import sqlite3
  5. noise = re.compile(r' {3,}.*')
  6. def __init_db__(path):
  7. con = sqlite3.connect(path)
  8. cur = con.cursor()
  9. cur.execute('''
  10. CREATE TABLE IF NOT EXISTS `transactions` (
  11. `details` STRING,
  12. `date` STRING,
  13. `amount` REAL,
  14. `type` STRING,
  15. `cOs_num` INTEGER,
  16. UNIQUE(
  17. `details`,
  18. `date`,
  19. `type`,
  20. `amount`,
  21. `cOs_num`));''')
  22. con.commit()
  23. return con
  24. class browser:
  25. def __init__(self,db_path):
  26. self.db_path = db_path
  27. self.con = __init_db__(db_path)
  28. self.con.row_factory = sqlite3.Row
  29. self.cur = self.con.cursor()
  30. def update_from_csv(self,csv_file):
  31. with open(csv_file) as file:
  32. reader = csv.DictReader(file)
  33. l = list(reader)
  34. for line in l:
  35. date = line.pop('Posting Date')
  36. cOs = line.pop('Check or Slip #')
  37. line['date'] = date_parse(date)
  38. line['Description'] = noise.sub('',line['Description'])
  39. line['Amount'] = float(line['Amount'])
  40. line['cOs'] = cOs
  41. self.cur.executemany('INSERT OR IGNORE INTO `transactions` VALUES (:Description,:date,:Amount,:Type,:cOs);',l)
  42. self.con.commit()
  43. if __name__ == "__main__":
  44. b = browser("test.db")
  45. b.update_from_csv('test.csv')
  46. res = b.cur.execute('SELECT * FROM transactions WHERE amount>=100;')