-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathdb.py
83 lines (60 loc) · 1.72 KB
/
db.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
import sqlite3
from datetime import datetime
import time
from dateutil import format_tran_date_for_db
db = None
def init_db():
global db
db = sqlite3.connect('./export/transactions.db')
db.execute('''
create table if not exists transactions
(
id integer primary key autoincrement,
timestamp DATETIME DEFAULT CURRENT_TIMESTAMP,
date text,
payer text,
amount text,
memo text,
payee text
)''')
return db
def save_transaction(t):
global db
db.execute('insert into transactions (date, payer, amount, memo, payee) values (?, ?, ?, ?, ?)',
(
format_tran_date_for_db(t.date),
t.payer,
t.amount,
t.memo,
t.payee
)
)
db.commit()
def get_only_new_transactions(trans):
res = []
for t in trans:
if not is_transaction_in_db(t):
res.append(t)
return res
def save_transactions(trans):
for t in trans:
save_transaction(t)
def is_transaction_in_db(t):
global db
cur = db.execute('''
select * from transactions where
date = ?
and payer = ?
and amount = ?
and memo = ?
and payee = ?
''',
(format_tran_date_for_db(t.date),
t.payer,
t.amount,
t.memo,
t.payee))
row = cur.fetchall()
if not row:
return False
return True