forked from supermistral/project
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfunction.py
More file actions
64 lines (54 loc) · 2.33 KB
/
function.py
File metadata and controls
64 lines (54 loc) · 2.33 KB
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
import sqlite3 as sql
import calendar
import datetime
#user = input()
class SQL:
def __init__(self, user):
self.User = 'name'+str(user)
def open_conn(self):
self.conn = sql.connect("base_db.sqlite")
self.cur = self.conn.cursor()
return self.conn, self.cur
def close_conn(self, conn, cur):
self.conn.commit()
self.cur.close()
self.conn.close()
def get_conn(self):
conn, cur = self.open_conn()
cur.execute("CREATE TABLE IF NOT EXISTS %s (income INTEGER, residue INTEGER, daily_res INTEGER, data TEXT)" %self.User)
self.close_conn(conn, cur)
def add_income(self, value):
conn, cur = self.open_conn()
cur.execute("UPDATE %s SET income = %s WHERE data = %s" %(self.User, value, str(datetime.datetime.now())[:10].replace('-', '')))
self.close_conn(conn, cur)
def add_residue(self, value):
conn, cur = self.open_conn()
cur.execute("UPDATE %s SET residue = %s WHERE data = %s" %(self.User, value, str(datetime.datetime.now())[:10].replace('-', '')))
self.close_conn(conn, cur)
def add_data(self):
conn, cur = self.open_conn()
temp = str(datetime.datetime.now())[:10].replace('-', '')
print(temp)
cur.execute("INSERT INTO %s (data) VALUES (%s)" %(self.User, temp))
self.close_conn(conn, cur)
def add_result(self):
conn, cur = self.open_conn()
now = datetime.datetime.now()
temp = str(now)[:10].replace('-', '')
days = calendar.monthrange(now.year, now.month)[1]
cur.execute("SELECT (income) FROM %s WHERE data = %s" %(self.User, temp))
for i in cur:
temp_income = i[0]
cur.execute("SELECT (residue) FROM %s WHERE data = %s" %(self.User, temp))
for i in cur:
temp_residue = i[0]
cur.execute("UPDATE %s SET daily_res = %d" %(self.User, (temp_income - temp_residue)/days))
self.close_conn(conn, cur)
def select_result(self):
conn, cur = self.open_conn()
cur.execute("SELECT (daily_res) FROM %s WHERE data = %s" %(self.User, str(datetime.datetime.now())[:10]))
for i in cur:
daily_res_send = i[0]
if cur.fetchall() == (): daily_res_send = 'Неполный ввод'
self.close_conn(conn, cur)
return daily_res_send