-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmodules.py
93 lines (76 loc) · 2.83 KB
/
modules.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
87
88
89
90
91
92
93
from uuid import uuid4
import sqlite3 as sql
import re
# generate uuids
def generate_uuid():
unique_id = str(uuid4())
return unique_id
# function to convert the seconds into readable format
def convert(seconds):
hours = seconds // 3600
seconds %= 3600
mins = seconds // 60
seconds %= 60
return hours, mins, seconds
# clean text function
def clean_text(text):
return re.sub('[!?.,\'()"]', '', text.lower())
# Create a specific current-user class/keeps log of user specific data
class Webuser():
def __init__(self):
self.uuid = ""
self.username = ""
self.myalbums = []
# SQL database manager code
class DB():
def __init__(self, file):
self.file = file
def insert(self, table, data):
# -> data = (username, uuid)
# Connect to the database
conn = sql.connect(self.file)
no_p = len(data)
value_space = "(" + "?" + ", ?"*(no_p - 1) + ")"
# Insert a row into the table using string formatting
query = f"INSERT INTO {table} VALUES {value_space}"
conn.execute(query, (data))
# Commit the changes to the database
conn.commit()
# Close the database connection
conn.close()
def read(self, table, data, many=False):
# Connect to the database
conn = sql.connect(self.file)
if data and not many:
# Retrieve a single row from the customers table
query = "SELECT * FROM {} WHERE {} = ?".format(table, data[0])
result = conn.execute(query, (data[1],)).fetchone()
elif data and many:
# Retrieve a single row from the customers table
query = "SELECT * FROM {} WHERE {} = ?".format(table, data[0])
result = conn.execute(query, (data[1],)).fetchall()
if not data and many:
query = "SELECT * FROM {}".format(table)
result = conn.execute(query).fetchall()
# print(result) # prints the tuple containing the customer information
# Close the database connection
conn.close()
return result
def remove(self, table, data):
# -> data = (column_name, row_value_to_delete)
# Connect to the database
conn = sql.connect(self.file)
# Delete the rows
query = "DELETE FROM {} WHERE {} = ?".format(table, data[0])
conn.execute(query, (data[1],))
# Commit the changes to the database
conn.commit()
# Close the database connection
conn.close()
###--- TEST CODE ---###
# uuid = generate_uuid() # GENERATE - ID
# print(uuid)
# db = DB("./db/user_pwd.db") # LOAD
# db.insert("user_uuid", ("ribin", uuid)) # SAVE
# db.read("user_uuid", ("username", "ribin")) # READ
# db.remove("user_uuid", ("username", "ribin")) # DELETE