-
Notifications
You must be signed in to change notification settings - Fork 2
/
data.py
71 lines (62 loc) · 2.7 KB
/
data.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
# importing the connector for setting up connection with mysql
import mysql.connector
class DataBase:
# constructor of DataBase class
def __init__(self):
# setting up the connection
self.conn = mysql.connector.connect(host='localhost',password='',user='root')
if self.conn.is_connected():
print("Connection is established...!")
else:
print("Connection is not established")
# getting the cursor from the connection object
self.cur = self.conn.cursor()
# creating the database named 'majorproject' if it do not exist
self.cur.execute("CREATE DATABASE IF NOT EXISTS majorproject;")
self.cur.execute("USE majorproject;")
# creating the table userdata
# three columns: email, pass and hash
self.cur.execute("CREATE TABLE IF NOT EXISTS userdata (email varchar(255) PRIMARY KEY, pass VARCHAR(16),hash CHAR(64));")
# function to check whether the email is already present in database or not
def isExistingUser(self,email):
# query to fetch the result if that user exists in the table
checkQuery = "SELECT * FROM userdata WHERE email = %s"
self.cur.execute(checkQuery, (email,))
existing_user = self.cur.fetchone()
# if exists then true
if existing_user :
return True
return False
# function to insert a new record to the table
def insertUser(self,email,password,hash):
if self.isExistingUser(email):
return False
else:
# Inserting new user into the table
insert_query = "INSERT INTO userdata (email, pass,hash) VALUES (%s, %s,%s)"
self.cur.execute(insert_query, (email, password,hash))
self.conn.commit()
return True
# function to authenticate user
def authenticateUser(self,email, password):
# matching the user's credentials in the table
query = "SELECT * FROM userdata WHERE email = %s AND pass = %s"
self.cur.execute(query, (email, password))
user = self.cur.fetchone()
if user:
return True
return False
# function to update the hash value in the table userdata
def updateHash(self,email,hash):
query = "UPDATE userdata SET hash = %s WHERE email = %s"
self.cur.execute(query,(hash,email))
self.conn.commit()
# function to get the last hash present against that user
def last_hash(self,username):
query = "SELECT hash from userdata where email = %s"
self.cur.execute(query,(username,))
result = self.cur.fetchall()
res = ""
for r in result:
res = r
return res[0]