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.

49 lines
1.5 KiB

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