I am making an user class which I could inherit and use in the future.
(recently started learning Python and OOP)
import mysql.connector as mycon
class user:
host = "localhost"
user = "root"
password = "pass"
database = "USERS"
table = "UserData"
def __init__(self,firstName,lastName,userName,email):
self.firstName = firstName
self.lastName = lastName
self.userName = userName
self.fullName = firstName + " " + lastName
self.email = email
@classmethod
def login(cls,userName,password):
db = mycon.connect(host = cls.host,user = cls.user,password = cls.password,database = cls.database)
cursor = db.cursor()
r=cursor.execute("SELECT * FROM {} WHERE UserName = '{}';".format(cls.table,userName))
userInfo = cursor.fetchone()
if(userInfo!=None):
if(userInfo[2]==password):
print("Login Successful")
return cls(userInfo[0],userInfo[1],userInfo[3],userInfo[4])
else:
print("INVALID PASSWORD",password,userInfo[2])
else:
print("USER DOES NOT EXIST")
db.close()
@classmethod
def register(cls,firstName,lastName,password,userName,email):
db = mycon.connect(host = cls.host,user = cls.user,password = cls.password,database = cls.database)
cursor = db.cursor()
r=cursor.execute("Insert into {} values('{}','{}','{}','{}','{}');".format(cls.table,firstName,lastName,password,userName,email))
db.commit()
db.close()
with a table UserData with description