0% found this document useful (0 votes)
17 views

LabManual(14-17)

Uploaded by

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

LabManual(14-17)

Uploaded by

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

Python

Interface Program
with
SQL
EXPT NO: 14 R
CREATING DATABASE AND TABLE USING MYSQL INTERFACE
Creating a python program to integrate MySQL with Python(Creating
Database and Table)

AIM:
To write a python program to integrate MySQL with Python
Creating Database and Table to store details of Employees.

Source Code:

import mysql.connector as ms
def create_DB():
db = ms.connect(host="localhost", user = "root", passwd = "Admin@123")
try:
if db.is_connected():
cur = db.cursor()
q = 'CREATE DATABASE Employees'
cur.execute(q)
print('Employees Database created successfully')
except:
print ('Database name already exists')
db.close()
def create_table():
db = ms.connect(host="localhost", user="root", passwd = "Admin@123",
database = "Employees")
try:
if db.is_connected():
cur = db.cursor()
q = 'CREATE TABLE EMP(ENo Int Primary Key, Ename Varchar(20),
Gender Varchar(3), salary Int)'
cur.execute(q)
print ('EMP table created successfully')
except:
print('Table Name already exixts')
db.close()
ch = 'y'
while ch =='y' or ch == 'Y':
print('\n Interfacing 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('Do you want to perform another operation(y/n): ')
EXPT NO: 14 R

Result:
Thus the above Python Program has been executed and the output
is verified successfully.
Expt No: 14 L
OUTPUT:
Interfacing Python with MySql
1. To Create Database
2. To Create Table
Enter your choice: 1
Employees Database created successfully
Do you want to perform another operation(y/n): Y

Interfacing Python with MySql


1. To Create Database
2. To Create Table
Enter your choice: 2
EMP table created successfully
Do you want to perform another operation(y/n): Y

Interfacing Python with MySql


1. To Create Database
2. To Create Table
Enter your choice: 3
Invalid Choice
Do you want to perform another operation(y/n): N
EXPT NO: 15 R
INSERTING AND DISPLAYING RECORDS USING MYSQL
INTERFACE
Creating a python program to integrate MySQL with Python(Inserting
Records and Displaying Records)

AIM:
To write a python program to integrate MySQL with Python by
inserting records to EMP table and display the records.

Source Code:

import mysql.connector as ms
db = ms.connect(host="localhost", user = "root", passwd = "Admin@123",
database = "Employees")
if db.is_connected():
cur = db.cursor()
opt = 'Y'
while opt == 'Y':
No = int (input ('Enter Employee Number: '))
Name = input ('Enter Employee Name: ')
Gender = input ('Enter Employee Gender(M/F): ')
Salary = int (input ('Enter Employee Salary: '))
Query = 'INSERT INTO EMP VALUES ({}, "{}", "{}", {})'.format(No,
Name, Gender, Salary)
cur.execute(Query)
db.commit()
print ('Record stored successfully')
opt = input ('Do you want to add another employee details (Y/N): ')
Query = "SELECT * FROM EMP"
cur.execute(Query)
data = cur.fetchall()
for i in data:
print(i)
db.close

Result:
Thus the above Python Program has been executed and the output
is verified successfully.
Expt No: 15 L
OUTPUT:
Enter Employee Number: 01
Enter Employee Name: Raja
Enter Employee Gender(M/F): M
Enter Employee Salary: 20000
Record stored successfully
Do you want to add another employee details (Y/N): Y
Enter Employee Number: 02
Enter Employee Name: Banu
Enter Employee Gender(M/F): F
Enter Employee Salary: 21000
Record stored successfully
Do you want to add another employee details (Y/N): Y
Enter Employee Number: 03
Enter Employee Name: Yoshua
Enter Employee Gender(M/F): M
Enter Employee Salary: 25000
Record stored successfully
Do you want to add another employee details (Y/N): N
(1, 'Raja', 'M', 20000)
(2, 'Banu', 'F', 21000)
(3, 'Yoshua', 'M', 25000)
EXPT NO: 16 R
SEARCHING AND DISPLAYING RECORDS USING MYSQL
INTERFACE
Creating a python program to integrate MySQL with Python(Searching
and Displaying Records)

AIM:
To write a python program to integrate MySQL with Python to
search an Employee using EMPID and display the record if present in
already existing table EMP. If not, display the appropriate message.

Source Code:

import mysql.connector as ms
db = ms.connect(host="localhost", user = "root", passwd = "Admin@123" ,
database = "Employees")
if db.is_connected():
cur = db.cursor()
print ('*********************************')
print ('Welcome to Employee Search Screen')
print ('*********************************')
No = int(input('Enter the employee number to search: '))
Query = 'SELECT * FROM EMP WHERE ENo = {}'.format(No)
cur.execute(Query)
data = cur.fetchone()
if data != None:
print(data)
else:
print('Record not found!!!')
db.close()

Result:
Thus the above Python Program has been executed and the output
is verified successfully.
Expt No: 16 L
OUTPUT:
*********************************
Welcome to Employee Search Screen
*********************************
Enter the employee number to search: 03
(3, 'Yoshua', 'M', 25000)

*********************************
Welcome to Employee Search Screen
*********************************
Enter the employee number to search: 04
Record not found!!!
EXPT NO: 17 R
UPDATING RECORDS USING MYSQL INTERFACE
Creating a python program to integrate MySQL with Python(Updating
Records)

AIM:
To write a python program to integrate MySQL with Python to
search an Employee using EMPID and update the salary of an employee
if present in already existing table EMP. If not, display the
appropriate message.

Source Code:

import mysql.connector as ms
db = ms.connect(host="localhost", user = "root", passwd = "Admin@123",
database = "Employees")
if db.is_connected():
cur = db.cursor()
print ('****************************************')
print ('Welcome to Employee Detail Update Screen')
print ('****************************************')
No = int(input('Enter the employee number to Update: '))
Query = 'SELECT * FROM EMP WHERE ENo = {}'.format(No)
cur.execute(Query)
data = cur.fetchone()
if data != None:
print('Record found details are: ')
print(data)
ans = input('Do you want to update the salary of the abouce
employee(Y/N)?: ')
if ans == 'y' or ans == 'Y':
New_Sal = int (input('Enter the New Salary of an Employee: '))
Q1 = 'UPDATE EMP SET SALARY = {} WHERE ENo ={}'.format(New_Sal, No)
cur.execute(Q1)
print('Employee Salary Updated Successfully')
Q2 = 'SELECT * FROM EMP'
cur.execute (Q2)
data = cur.fetchall()
for i in data:
print(i)
else:
print('Record not found!!!')
db.close()

Result:
Thus the above Python Program has been executed and the output
is verified successfully.
Expt No: 17 L
OUTPUT:
****************************************
Welcome to Employee Detail Update Screen
****************************************
Enter the employee number to Update: 01
Record found details are:
(1, 'Raja', 'M', 20000)
Do you want to update the salary of the abouce employee(Y/N)?: Y
Enter the New Salary of an Employee: 24000
Employee Salary Updated Successfully
(1, 'Raja', 'M', 24000)
(2, 'Banu', 'F', 21000)
(3, 'Yoshua', 'M', 25000)

You might also like