-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathdb_functions.py
86 lines (74 loc) · 2.32 KB
/
db_functions.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
84
85
86
import sqlite3
def get_db_connection():
"""Return a new connection to the database."""
return sqlite3.connect("currencies.db")
def create_table():
"""Create the tgju table if it doesn't exist."""
try:
with get_db_connection() as con:
cur = con.cursor()
cur.execute("""
CREATE TABLE IF NOT EXISTS tgju(
dollar TEXT,
pound TEXT,
euro TEXT,
created_at DATETIME DEFAULT CURRENT_TIMESTAMP
)
""")
except sqlite3.Error as e:
print(f"An error occurred: {e}")
finally:
if con:
con.close()
def add_row(usd, gbp, eur):
"""Add a new row to the tgju table."""
try:
with get_db_connection() as con:
cur = con.cursor()
cur.execute(
"INSERT INTO tgju VALUES (?, ?, ?, CURRENT_TIMESTAMP)", (usd, gbp, eur)
)
except sqlite3.Error as e:
print(f"An error occurred: {e}")
finally:
if con:
con.close()
def retrieve_dollar():
"""Retrieve the latest dollar value from the tgju table."""
try:
with get_db_connection() as con:
cur = con.cursor()
cur.execute("SELECT dollar FROM tgju ORDER BY rowid DESC LIMIT 1")
dollar = cur.fetchone()[0]
except sqlite3.Error as e:
print(f"An error occurred: {e}")
finally:
if con:
con.close()
return dollar
def retrieve_pound():
"""Retrieve the latest pound value from the tgju table."""
try:
with get_db_connection() as con:
cur = con.cursor()
cur.execute("SELECT pound FROM tgju ORDER BY rowid DESC LIMIT 1")
pound = cur.fetchone()[0]
except sqlite3.Error as e:
print(f"An error occurred: {e}")
finally:
if con:
con.close()
return pound
def retrieve_euro():
"""Retrieve the latest euro value from the tgju table."""
try:
with get_db_connection() as con:
cur = con.cursor()
cur.execute("SELECT euro FROM tgju ORDER BY rowid DESC LIMIT 1")
euro = cur.fetchone()[0]
except sqlite3.Error as e:
print(f"An error occurred: {e}")
finally:
if con:
con.close()
return euro