-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsqlCode.py
More file actions
126 lines (104 loc) · 3.78 KB
/
sqlCode.py
File metadata and controls
126 lines (104 loc) · 3.78 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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
import sqlite3
DB_NAME = "UsersDataBase.db"
SALT = "MJS12345433"
def get_database_connection(): # a Connection to the database has to be established before any query is executed
con = sqlite3.connect(DB_NAME)
return con
def test_execute(connection, query):
cursor = connection.cursor()
for row in cursor.execute(query):
print(row)
return True
def execute_query(connection, query):
cursor = connection.cursor()
try:
cursor.execute(query) # gets the query as a paramiter then executes it from the connection
connection.commit()
print("Query executed successfully")
return cursor.fetchone()
except sqlite3.Error as e:
if e == 'UNIQUE constraint failed: users.email':
print("Email not unique")
return False
else:
print(f"The error '{e}' occurred") # SQL doesnt print console errors hence the self error printing
return False
def execute_select_query(connection, query):
cursor = connection.cursor()
cursor.execute(query)
connection.commit()
return execute_query(connection, query)
def create_database(connection):
try:
# r"C:\Users\ruthr\PycharmProjects\Alevel2020Coursework\
# Windows version
try:
file = open(r'/Users/harrisonrigby/PycharmProjects/Alevel2020_Coursework/Schema') # the schemea needs to
# be opened for the database to be creaetd
except FileNotFoundError:
file = open("Schema")
print("file not found")
query = file.read()
connection.executescript(query)
except sqlite3.Error as e:
print(f"The error '{e}' occurred")
def username_validation(connection, username):
# will take the inputted username and check if there is a email associated to validata it is in the database
query = f"SELECT email FROM users WHERE username == ('{username}') ;"
tables = execute_query(connection, query)
tables_list = []
try:
for item in tables:
tables_list.append(item)
print(tables_list)
print("invalid username")
return True
except:
print("valid username")
return False
def email_validation(connection, email):
# will take the user email and check if there is a username associated to validata it is in the database
query = f"SELECT username FROM users WHERE email == ('{email}') ;"
tables = execute_query(connection, query)
tables_list = []
try:
for item in tables:
tables_list.append(item)
print(tables_list)
print("valid email")
return True
except:
print("invalid email")
return False
def validation(connection, query):
tables = execute_query(connection, query)
tables_list = []
try:
for item in tables:
tables_list.append(item[0])
print(tables_list)
return True
except:
return False
def username_and_password_validation(connection, username, hashedPassword):
# will take the username and a hashed password and compares the hashed password in the database to the hashed
# password taken in to authroise the user
query = f"SELECT password FROM users WHERE username == ('{username}') ;"
tables = execute_query(connection, query)
tables_list = []
try:
print(tables_list)
for item in tables:
tables_list.append(item)
if hashedPassword == item:
return True
else:
print("username not matching password")
return False
except:
print("username not in system")
return False
if __name__ == '__main__':
print(".")
email_validation(get_database_connection(), 'harrisonrigby@icloud.com')
create_database(get_database_connection())