import mysql.connector as connector
class DbHelper:
def __init__(self):
self.conn = connector.connect(host = '127.0.0.1', user = 'root', password = '', database = 'python_test')
query = 'create table if not exists user(userID int primary key, firstname varchar(200),lastname varchar(200), Dob varchar(15), contact varchar(10), address varchar(100))'
curr = self.conn.cursor()
curr.execute(query)
print()
def insert_Data(self, userID, firstname, lastname, Dob, contact, address):
query = "insert into user (userID, firstname, lastname, Dob, contact, address) values({},'{}','{}','{}','{}','{}')".format(userID,firstname, lastname, Dob, contact, address)
curr = self.conn.cursor()
curr.execute(query)
self.conn.commit()
print(query)
print("User data inserted....")
def display_Data(self):
query = 'select * from user'
print()
curr = self.conn.cursor()
curr.execute(query)
for row in curr:
print('userID :', row[0])
print('firstname :', row[1])
print('lastname :', row[2])
print('Dob :', row[3])
print('contact :', row[4])
print('address :', row[5])
print()
print()
def delete_user(self, userID):
query = "DELETE FROM user WHERE userID={}".format(userID)
curr = self.conn.cursor()
curr.execute(query)
self.conn.commit()
print("Data delete for UserID " +str(userID))
def update_Data(self, userID, newfirstname, newlastname, newDob, newcontact, newaddress):
query = "update user set firstname = '{}', lastname ='{}', Dob = '{}', contact = '{}', address = '{}' where userID = {}".format(newfirstname, newlastname, newDob, newcontact, newaddress,userID)
curr = self.conn.cursor()
curr.execute(query)
self.conn.commit()
print()
print("Data updated for UserID " +str(userID))