0% found this document useful (0 votes)
7 views2 pages

Python SQL Interface

The document outlines how to interface Python with MySQL to perform operations on an 'EMPLOYEE' table in a 'BANK' database. It includes code snippets for displaying male staff names, inserting new employee data, updating salaries for the accounts department, and deleting records of employees with salaries below 20,000. Each operation demonstrates the use of the mysql.connector library for database connectivity and manipulation.

Uploaded by

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

Python SQL Interface

The document outlines how to interface Python with MySQL to perform operations on an 'EMPLOYEE' table in a 'BANK' database. It includes code snippets for displaying male staff names, inserting new employee data, updating salaries for the accounts department, and deleting records of employees with salaries below 20,000. Each operation demonstrates the use of the mysql.connector library for database connectivity and manipulation.

Uploaded by

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

INTERFACING PYTHON WITH MYSQL

Objective:
To write programs to connect python with mysql using database connectivity
and perform following operations on “EMPLOYEE” table in “BANK” database:

1. To display the names of all male staffs in employee table.

import mysql.connector as ms
mydb = ms.connect(host = “localhost”, user = “root”, passwd = “admin”, database =
“bank”)
if mydb.is_connected():
print(“Connection successful”)
mycursor = mydb.cursor()
sq = “select ename from employee where gender = ‘{ }’ “ . format(‘M’)
mycursor.execute(sq)
data = cursor.fetchall()
for row in data:
print(row)
mydb.close()

2. To insert new data into employee table:

import mysql.connector as ms
mydb = ms.connect(host = “localhost”, user = “root”, passwd = “admin”, database =
“bank”)
if mydb.is_connected():
print(“Connection successful”)
mycursor = mydb.cursor()
sq = “insert into employee values ({ }, ‘{ }’, ‘{ }’, ‘{ }’, ‘{ }’, ‘{ } ‘, { })” . format(1234,
‘Riya’ , ‘IT’, ‘Delhi’, ‘F’, ‘1996-10-12’, 30000.00)
mycursor.execute(sq)
mydb.commit()
mydb.close()
3. To update the salary of all employees of accounts department by incrementing
salary by 5000.

import mysql.connector as ms
mydb = ms.connect(host = “localhost”, user = “root”, passwd = “admin”, database =
“bank”)
if mydb.is_connected():
print(“Connection successful”)
mycursor = mydb.cursor()
sq = “update employee set salary = salary + {} where dept = ‘{ }’ . format(5000, ‘IT)
mycursor.execute(sq)
mydb.commit()
mydb.close()

4. To delete the records of all employees whose salary is less than 20000.
import mysql.connector as ms
mydb = ms.connect(host = “localhost”, user = “root”, passwd = “admin”, database =
“bank”)
if mydb.is_connected():
print(“Connection successful”)
mycursor = mydb.cursor()
sq = “delete from employee where salary < 20000.00”
mycursor.execute(sq)
mydb.commit()
mydb.close()

You might also like