This repository was archived by the owner on May 29, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdb.py
More file actions
60 lines (49 loc) · 1.64 KB
/
db.py
File metadata and controls
60 lines (49 loc) · 1.64 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
import sqlite3
import datetime
def connect():
connection = sqlite3.connect("db.db")
cursor = connection.cursor()
return connection, cursor
def execute(connection, cursor, quary, args):
cursor.execute(quary, args)
connection.commit()
connection.close()
def quarry(connection, cursor, query, args={}, one=False):
cursor.execute(query, args)
cursor.row_factory = sqlite3.Row
rv = cursor.fetchall()
connection.close()
return (rv if rv else None) if one else rv
def find_date(date):
connection, cursor = connect()
find = quarry(connection, cursor, "SELECT date FROM graph WHERE date = ?", [date], one=True)
if find != None and len(find) > 0:
return True
else:
return False
def read(date):
connection, cursor = connect()
value = 0
for q in quarry(connection, cursor, "SELECT * FROM graph WHERE date = ?", [date]):
value = q[2]
if value == 0:
return 0
else:
return value
def write():
connection, cursor = connect()
date = datetime.datetime.now().strftime("%Y%m%d")
temp = int(read(date))
if find_date(date):
value = temp + 1
execute(connection, cursor, "UPDATE graph SET value = ? WHERE date = ?", [value, date])
else:
execute(connection, cursor, "INSERT INTO graph (date, value) VALUES (?, ?)", [date, 1])
def output():
connection, cursor = connect()
dates = []
values = []
for value in quarry(connection, cursor, "SELECT * FROM graph WHERE NOT id = ?", ["0"]):
dates.append(value[1][6:8] + "/" + value[1][4:6] + "/" + value[1][:4])
values.append(value[2])
return dates, values