You are on page 1of 4

# INSERTING DATA

import mysql.connector as mys


mycon = mys.connect(host='localhost', user='root', passwd='mysql', database='12a_2023')
mycursor = mycon.cursor()
print ("Welcome to Employee Data Entry ")
ans='y'
while ans=='y':
eno= int(input ("Enter employee no. :"))
nm= input ("Enter Name :")
dp = input ("Enter Department :")
s=(input ("Enter Salary :"))
query="insert into emp values ({0}, '{1}', '{2}', {3})".format (eno, nm, dp, s)
mycursor.execute(query)
mycon.commit()
print("## Record Saved... #")
ans=input("Add more?")

# UPDATING DATA
import mysql.connector as mys
mycon=mys.connect(host='localhost', user='root', passwd='mysql', database='12a_2023')
mycursor = mycon.cursor()
print("Welcome to Employee Update Screen ")
eno=int(input("Enter employee number :"))
query="select * from emp where empno={}".format(eno)
mycursor.execute(query)
data= mycursor.fetchone()
if data!=None:
print("## Record Found - Details are ##")
print(data )
ans = input ("Are you sure to update NAME (y/n):")
if ans=='y':
nm=input("Enter new name :")
query="update emp set nm={} where empno={}".format(nm,eno)
mycursor.execute(query)
mycon.commit()
print("## Record updated ##")
else:
print ("Sorry! No Such Empno exists")
mycon.close()
# SEARCHING DATA
Pip install mysql.connector

import mysql.connector as mys


mycon=mys.connect(host='localhost', user='root', passwd='mysql', database='12a_2023')
mycursor = mycon.cursor()
eno=int(input("Enter employee number to search : "))
query="select * from emp where empno="+str(eno)
mycursor.execute(query)
data=mycursor.fetchone()
if data!=None:
print(data)
else:
print("No such employee Number :")

MYSQL QUERY FOR 2 TABLES

I)
EMPLOYEEID NAME SALES JOBID
E1 SUMIT SINGH
E2 VIJAY
E3
E4
E5
E6

1. To display employee name of employee, jobid with the corresponding job title.
→ mysql> SELECT EMPLOYEEID,NAME,EMPLOYEE.JOBID,JOBTITLE
-> FROM EMPLOYEE,JOB
-> WHERE EMPLOYEE.JOBID=JOB.JOBID;

2. To display names of employee sales, and corresponding job title who have achieve sales more
than 13 lakhs.
→ mysql> SELECT NAME,SALES,JOBTITLE
-> FROM EMPLOYEE,JOB
-> WHERE EMPLOYEE.JOBID=JOB.JOBID
-> AND SALES > 1300000;

3. To display name and corresponding name title of those employee who have ‘sigh’ anywhere in
their names.
→mysql> SELECT NAME,JOBTITLE
-> FROM EMPLOYEE,JOB
-> WHERE EMPLOYEE.JOBID=JOB.JOBID
-> AND NAME LIKE "%SINGH%";

4. Write the command to change the jobid to 104 of the employee with ID as ‘E4’ in the employee
table?
→mysql> update employee
-> set jobid=104
-> where employeeid like "%E4%";

II)
1. To display average price of all the uniform of Raymond company from the table cost.
→ mysql> SELECT AVG(PRICE)
-> FROM COST
-> WHERE COMPANY = "RAYMOND";
2. To display details of all the uniform from the ‘UNIFORM TABLE’ in the descending order of stock
date.
→ mysql> SELECT * FROM UNIFORM
-> ORDER BY STOCKDATE DESC;

3. To display max price and min price of each company.


→ mysql> SELECT COMPANY,MAX(PRICE),MIN(PRICE)
-> FROM COST
-> GROUP BY COMPANY;

4. To display company where the number of uniform size is ‘M’.


→ mysql> SELECT COMPANY,COUNT(SIZE)
-> FROM COST
-> GROUP BY COMPANY
-> HAVING COUNT(SIZE)>2;

5. To display the uniform code, name, colour, size and company.


→ mysql> SELECT UNIFORM.UCODE,UNAME,UCOLOUR,SIZE,COMPANY
-> FROM UNIFORM,COST
-> WHERE UNIFORM.UCODE=COST.UCODE;

III)
Create database and table with python
CODE:
import mysql.connector
def Create_DB():

Con=mysql.connector.connect(host="localhost",user="root",passwd="mysql",database="employees
")
try:
if Con.is_connected():
cur=Con.cursor()
Q="CREATE DATABASE employee"
cur.execute(Q)
print("Employees database created sucessfully")
except:
print("Database name already exist")
Con.close()
def Create_Table():

Con=mysql.connector.connect(host="localhost",user="root",passwd="mysql",database="employees
")
if Con.is_connected():
cur=Con.cursor()
Q="CREATE TABLE EMPLOYEE(ENO INT PRIMARY KEY,ENAME
VARCHAR(20),GENDER VARCHAR(3), SALARY INT)"
cur.execute(Q)
print("employee table created sucessfully")
else:
print("table name already exist")
Con.close()
ch="y"
while ch == "y" or ch == "Y":
print("\nInterfacing Python with MySQL")
print("1. To Create Database")
print("2. To Create Table ")
opt=int(input("Enter your choice : "))
if opt==1:
Create_DB()
elif opt == 2:
Create_Table()
else:
print("Invalid choice")
ch=input("Want to continue(y/n) : ")

You might also like