-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdatabase.py
More file actions
109 lines (93 loc) · 3.57 KB
/
database.py
File metadata and controls
109 lines (93 loc) · 3.57 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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
# database.py
"""This module is made to provide easy functions for my @AAU_Robot.
This module provides function for creating, deleting table, inserting data
deleting and from the table."""
import sqlite3
from datetime import date
def create_table() -> None:
"""Creates a table; this is usually performed once."""
try:
with sqlite3.connect('student.sql') as database:
cursor = database.cursor()
create = """
CREATE TABLE IF NOT EXISTS Student (
TGID TEXT PRIMARY KEY NOT NULL,
IDNO TEXT NOT NULL,
USERNAME TEXT NOT NULL,
CAMPUS TEXT NOT NULL,
JOINDATE DATE NOT NULL
)
"""
cursor.execute(create)
except sqlite3.Error as error:
print(error)
def insert_data(values: tuple) -> None:
"""Inserts user information into the database."""
try:
with sqlite3.connect('student.sql') as database:
cursor = database.cursor()
insertdata = """
INSERT INTO Student (TGID, IDNO, USERNAME, CAMPUS, JOINDATE)
VALUES (?, ?, ?, ?, ?)
"""
cursor.execute(insertdata, values)
database.commit()
except sqlite3.Error as error:
print(error)
def registered_count() -> int:
"""Function that gets the number of tuples in the table."""
with sqlite3.connect('student.sql') as database:
cursor = database.cursor()
select_all = """SELECT TGID FROM Student"""
cursor.execute(select_all)
table = cursor.fetchall()
return len(table)
def registered_today() -> int:
"""Function that gets the number of tuples with today's date in the table."""
today = date.today().strftime("%d/%m/%Y")
with sqlite3.connect('student.sql') as database:
cursor = database.cursor()
select = """SELECT * FROM Student WHERE JOINDATE = ?"""
cursor.execute(select, (today,))
table = cursor.fetchall()
return len(table)
def search_table_by_tg_id(tg_id) -> tuple:
"""Search for a record based on TGID and return one record if found."""
try:
with sqlite3.connect('student.sql') as database:
cursor = database.cursor()
search = """SELECT * FROM Student WHERE TGID = ?"""
cursor.execute(search, (tg_id,))
row = cursor.fetchone()
return row
except sqlite3.Error as e:
print(e)
def delete_table_data():
"""Delete all records in the Student table."""
try:
with sqlite3.connect('student.sql') as database:
cursor = database.cursor()
delete = """DELETE FROM Student"""
cursor.execute(delete)
database.commit()
except sqlite3.Error as error:
print(error)
def delete_from_table(key):
"""Delete a specific user from the table based on TGID."""
try:
with sqlite3.connect('student.sql') as database:
cursor = database.cursor()
delete_row = """DELETE FROM Student WHERE TGID=?"""
cursor.execute(delete_row, (key,))
except sqlite3.Error as e:
print(e)
def modify_idno(tg_id, new_idno) -> None:
"""Modify the IDNO for a specific user based on TGID."""
try:
with sqlite3.connect('student.sql') as database:
cursor = database.cursor()
update_idno = """UPDATE Student SET IDNO=? WHERE TGID=?"""
cursor.execute(update_idno, (new_idno, tg_id))
database.commit()
except sqlite3.Error as e:
print(e)