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

XII Practical Book Programs 18-21

The document contains a series of Python programs that demonstrate how to connect to a MySQL database and perform various operations such as displaying databases, creating an employee table, inserting records, searching for an employee by number, and deleting a record. Each program includes code snippets and sample outputs to illustrate the functionality. The examples utilize the mysql.connector library to interact with the MySQL database.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views

XII Practical Book Programs 18-21

The document contains a series of Python programs that demonstrate how to connect to a MySQL database and perform various operations such as displaying databases, creating an employee table, inserting records, searching for an employee by number, and deleting a record. Each program includes code snippets and sample outputs to illustrate the functionality. The examples utilize the mysql.connector library to interact with the MySQL database.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

Program 18: Program to connect with MySql and display databases from current user.

#Show Databases

import mysql.connector
mydb=mysql.connector.connect(host="localhost",user="root",password="angel")
mycursor=mydb.cursor()
mycursor.execute("show databases")

for x in mycursor:
print(x)

Output:

('information_schema',)
('hari',)
('hari1',)
('mysql',)
('performance_schema',)
('test',)
Program 19: Write a python program to connect with MySql and create employee table with
given information like employee no, employee name, department and salary.
Insert given values into employee table.
(101,'Manish Mehta','Sales',50000)
(102, ‘Satish Patel’, ‘Marketing’,45000)
(103, ‘Minesh Joshi’ , ‘Purchase’, 40000)
Code:
import mysql.connector
mydb=mysql.connector.connect(host="localhost",user="root",
password="angel",database='school')
mycursor=mydb.cursor()
mycursor.execute("create table employee(eno int primary key,ename varchar(30),
dept varchar(30),sal decimal)")
mycursor.execute("insert into employee values
(101,'Manish Mehta', 'Sales',50000)")
mycursor.execute("insert into employee values
(102,'Satish Patel', 'Marketing',45000)")
mycursor.execute("insert into employee values
(103,'Minesh Joshi','Purchase', 40000)")
mycursor.execute("commit")
print("Record Saved Successfully…!")
OUTPUT :
Record Saved Successfully…!
Program 20: Write a python program to connect with MySql and search record of given
employee number from employee table. If employee number is not present than display
message 'Record Not Found'.
Code:

import mysql.connector
mydb=mysql.connector.connect(host="localhost",user="root",
password="angel",database='school')
mycursor=mydb.cursor()
eno=int(input('Enter Employee Number to be Search:'))
mycursor.execute("Select * from employee where ENO=%s",(eno,))
rec=mycursor.fetchall()

if rec==[]:
print('Record Not Found...!')
else:
row=rec[0]
print('Employee No:',row[0])
print('Employee Name:',row[1])
print('Employee Dept:',row[2])
print('Employee Salary:',row[3])

OUTPUT :

Enter Employee Number to be Search:102


Employee No: 102
Employee Name: Satish Patel
Employee Dept: Marketing
Employee Salary: 45000

Enter Employee Number to be Search:110


Record Not Found...!
Program 21: Write a python program to connect with MySql and delete record of given
employee number from employee table. If employee number is not present than display
message 'Record Not Found'.
Code:

import mysql.connector
mydb=mysql.connector.connect(host="localhost",user="root",
password="angel",database='school')
mycursor=mydb.cursor()
eno=int(input('Enter Employee Number to be Delete:'))
mycursor.execute("Delete from employee3 where ENO=%s",(eno,))
rec=mycursor.rowcount
if rec==0:
print('Record Not Found...!')
else:
mycursor.execute("commit")
print('Record Deleted Successfully...!')

mydb.close()

OUTPUT :

Enter Employee Number to be Delete:104


Record Deleted Successfully...!

Enter Employee Number to be Delete:106


Record Not Found...!

You might also like