0% found this document useful (0 votes)
43 views3 pages

Mysql Connectivity With Python

The document contains a Python script that connects to a MySQL database for managing employee records in a car showroom. It provides functionalities to insert, delete, update, and display employee information. The script includes a user interface for executing these operations in a loop until the user chooses to exit.

Uploaded by

Sunil Singh
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
43 views3 pages

Mysql Connectivity With Python

The document contains a Python script that connects to a MySQL database for managing employee records in a car showroom. It provides functionalities to insert, delete, update, and display employee information. The script includes a user interface for executing these operations in a loop until the user chooses to exit.

Uploaded by

Sunil Singh
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 3

import mysql.

connector as mycon

con = mycon.connect(host="localhost",
user="root",
password="linus@321",
database="carshowroom")

if con.is_connected():
print("Connection Successfully established")
else:
print("Error in connection")

cur = con.cursor()

def insert():
e = input("Enter Employee ID:")
en = input("Enter Employee Name:")
dob = input("Enter employee date of Birth (YYYY-MM-DD):")
doj = input("Enter employee date of Joining (YYYY-MM-DD):")
d = input("Enter employee designation:")
s = int(input("Enter employee salary:"))
queryinsert = "insert into employee values('{}','{}','{}','{}','{}',
{})".format(e, en, dob, doj, d, s)
cur.execute(queryinsert)
con.commit()
print("Data inserted successfully.")

def delete():
e = input("Enter Employee ID:")
querydelete="delete from employee where EmpID='{}'".format(e)
cur.execute(querydelete)
con.commit()
print("Row deleted successfully.")

def display():

# Fetching and printing data after changes


cur.execute("select * from employee")
data = cur.fetchall()
for i in data:
date_format_dob = i[2].strftime('%Y-%m-%d') if i[2] is not None else None
data_format_doj = i[3].strftime('%Y-%m-%d') if i[3] is not None else None
print(i[0], i[1], date_format_dob, data_format_doj, i[4], i[5])

def update():
e = input("Enter Employee ID:")

# Displaying the current record


cur.execute("SELECT * FROM employee WHERE EmpID='{}'".format(e))
current_data = cur.fetchone()
if current_data is None:
print("No employee found with ID {}".format(e))
return

print("Current Record:")
date_format_dob = current_data[2].strftime('%Y-%m-%d') if current_data[2] is
not None else None
data_format_doj = current_data[3].strftime('%Y-%m-%d') if current_data[3] is
not None else None
print(current_data[0], current_data[1], date_format_dob, data_format_doj,
current_data[4], current_data[5])

print("\nChoose an attribute to update:")


print("1. Employee ID")
print("2. Employee Name")
print("3. Date of Birth")
print("4. Date of Joining")
print("5. Designation")
print("6. Salary")
choice = int(input("Enter your choice:"))

if choice == 1:
new_value = input("Enter new Employee ID:")
queryupdate = "UPDATE employee SET EmpID='{}' WHERE
EmpID='{}'".format(new_value, e)
elif choice == 2:
new_value = input("Enter new Employee Name:")
queryupdate = "UPDATE employee SET EmpName='{}' WHERE
EmpID='{}'".format(new_value, e)
elif choice == 3:
new_value = input("Enter new Date of Birth (YYYY-MM-DD):")
queryupdate = "UPDATE employee SET DOB='{}' WHERE
EmpID='{}'".format(new_value, e)
elif choice == 4:
new_value = input("Enter new Date of Joining (YYYY-MM-DD):")
queryupdate = "UPDATE employee SET DOJ='{}' WHERE
EmpID='{}'".format(new_value, e)
elif choice == 5:
new_value = input("Enter new Designation:")
queryupdate = "UPDATE employee SET Designation='{}' WHERE
EmpID='{}'".format(new_value, e)
elif choice == 6:
new_value = int(input("Enter new Salary:"))
queryupdate = "UPDATE employee SET Salary={} WHERE
EmpID='{}'".format(new_value, e)
else:
print("Invalid choice.")
return

cur.execute(queryupdate)
con.commit()
print("Data Updated Successfully")

while True:
print("===========================================")
print("1. To insert new record in employee table.")
print("2. To delete record in employee table.")
print("3. To update record in employee table.")
print("4. To Dispaly employee table record")
print("5. To Exit")
ch = int(input("Enter your choice:"))
print("===========================================")
if ch == 1:
insert()
elif ch == 2:
delete()
elif ch == 3:
update()
elif ch == 4:
display()
elif ch==5:
print("Bye..............")
break

# Close the cursor and connection


cur.close()
con.close()

You might also like