forked from UTSAVS26/PyVerse
-
Notifications
You must be signed in to change notification settings - Fork 0
/
db.py
94 lines (76 loc) · 2.11 KB
/
db.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
94
import sqlite3
import random
import string
def create_table():
conn = sqlite3.connect('mydatabase.db')
cursor = conn.cursor()
create_table_query = '''
CREATE TABLE IF NOT EXISTS User_Authentication (
username TEXT PRIMARY KEY,
password TEXT
)
'''
cursor.execute(create_table_query)
conn.commit()
cursor.close()
conn.close()
def insert_user(username, password):
conn = sqlite3.connect('mydatabase.db')
cursor = conn.cursor()
try:
insert_query = '''
INSERT INTO User_Authentication (username, password)
VALUES (?, ?)
'''
cursor.execute(insert_query, (username, password))
conn.commit()
except sqlite3.IntegrityError:
cursor.close()
conn.close()
return False
cursor.close()
conn.close()
return True
def verify_credentials(username, password):
conn = sqlite3.connect('mydatabase.db')
cursor = conn.cursor()
select_query = '''
SELECT * FROM User_Authentication WHERE username = ? AND password = ?
'''
cursor.execute(select_query, (username, password))
result = cursor.fetchone()
cursor.close()
conn.close()
return result is not None
def reset_password(email, new_password):
conn = sqlite3.connect('mydatabase.db')
cursor = conn.cursor()
try:
# Update the user's password based on username (assuming username is email)
update_query = '''
UPDATE User_Authentication
SET password = ?
WHERE username = ?
'''
cursor.execute(update_query, (new_password, email))
conn.commit()
return True
except Exception as e:
print("Error resetting password:", str(e))
return False
finally:
cursor.close()
conn.close()
def show_all_users():
conn = sqlite3.connect('mydatabase.db')
cursor = conn.cursor()
select_all_query = '''
SELECT * FROM User_Authentication
'''
cursor.execute(select_all_query)
results = cursor.fetchall()
for row in results:
print(row)
cursor.close()
conn.close()
show_all_users()