0% found this document useful (0 votes)
23 views14 pages

Cs Pracfile12

The program creates a menu-driven interface to perform CRUD operations on MySQL databases and tables using Python and the mysql.connector module. The menu allows the user to 1) create a database, 2) show databases, 3) create a table, 4) insert a record, 5) update a record, 6) delete a record, and 7) display records. It connects to a MySQL database, defines functions for each operation, and calls the corresponding function based on the user's menu selection.

Uploaded by

techniteshgamer
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)
23 views14 pages

Cs Pracfile12

The program creates a menu-driven interface to perform CRUD operations on MySQL databases and tables using Python and the mysql.connector module. The menu allows the user to 1) create a database, 2) show databases, 3) create a table, 4) insert a record, 5) update a record, 6) delete a record, and 7) display records. It connects to a MySQL database, defines functions for each operation, and calls the corresponding function based on the user's menu selection.

Uploaded by

techniteshgamer
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/ 14

Question16:

Write a menu-driven program connecting with mysql menu:- 1.create a


database 2.show all databases 3.create a table 4.insert a record
5.update arecord 6.delete a record 7.display the records 8.exit

Program:

import mysql.connector as sql mydb=sql.connect(host="localhost",user="root",password=


"numerator")
def createdb(): cursor=mydb.cursor()

query1="create database if not exists exam"


cursor.execute(query1)
print("---SUCCESSFULLY CREATED THE DATABASE -------------------------- ")
cursor.close()def
showdb():
cursor=mydb.cursor()
query2="show databases"
cursor.execute(query2)
data1=cursor.fetchall()for i in
data1:
print(i)def

createtb():

mydb=sql.connect(host="localhost",user="root",passw
ord="numerator",database="exam")
cursor=mydb.cursor()
query3="create table if not exists classXII(rollno int primary key,name
varchar(20),marks float)"
cursor.execute(query3)mydb.commit()
print("---TABLE CREATED SUCCESSFULLY--")
cursor.close()def
insertrec():
mydb=sql.connect(host="localhost",user="root",passw
ord="numerator",database="exam")
cursor=mydb.cursor()while True:

roll=int(input('Enter rollno')) name=input('Enter


name') marks=float(input('Enter marks'))

query4="insert into classxii values({},'{}',{})".format(roll,name,marks)


cursor.execute(query4)
mydb.commit()
print("----INSERTED --------------------- ")
ch=input('Do you want to continue?')if ch=='n' or
ch=='N':
break
cursor.close()def
displayrec():
mydb=sql.connect(host="localhost",user="root",passw
ord="numerator",database="exam")
cursor=mydb.cursor() query5="select * from
classxii"cursor.execute(query5)
data2=cursor.fetchall()
for j in data2:print(j)
cursor.close()def
updaterec():
mydb=sql.connect(host="localhost",user="root",passw
ord="numerator",database="exam")
cursor=mydb.cursor()
r=int(input('Enter rollno to be updated')) m=float(input('Enter updated
marks')) query6="update classxii set marks={} where rollno={
}".format(m,r) cursor.execute(query6)
mydb.commit()
print("---UPDATED ------------------ ")
cursor.close()def
deleterec():
mydb=sql.connect(host="localhost",user="root",passw
ord="numerator",database="exam")
cursor=mydb.cursor()
r=input('Enter rollno whose record to be deleted') query7="delete from classxii
where rollno={}".forma
t(r,)
cursor.execute(query7)mydb.commit()
print("------DELETED ------------------------ ")
cursor.close()while
True:

print("1.Create a database") print("2.Show all


databases") print("3.Create a table") print("4.Insert
the record") print("5.Update record")
print("6.Delete record") print("7.Show the
records") print("8.Exit") ch=int(input("Enter your
choice="))if ch==1:
createdb()elif
ch==2:
showdb()
elif ch==3:
createtb()elif
ch==4:
insertrec()elif
ch==5:
updaterec()elif
ch==6:
deleterec()elif
ch==7:
displayrec()elif
ch==8:
break
else:
print('Invalid choice')
Output:
1.Create a database 2.Show
all databases3.Create a table
4.Insert the record 5.Update
record 6.Delete record
7.Show the records 8.Exit
Enter your choice=1
---SUCCESSFULLY CREATED THE DATABASE----
1.Create a database 2.Show
all databases3.Create a table
4.Insert the record 5.Update
record 6.Delete record
7.Show the records 8.Exit
Enter your choice=2 ('exam',)
('information_schema',)('mysql',)
('performance_schema',)('practical',)
('sys',)
1.Create a database 2.Show
all databases3.Create a table
4.Insert the record 5.Update
record 6.Delete record
7.Show the records 8.Exit
Enter your choice=3
---TABLE CREATED SUCCESSFULLY--
1.Create a database 2.Show
all databases3.Create a table
4.Insert the record
5.Update record 6.Delete
record 7.Show the records
8.Exit
Enter your choice=4Enter
rollno2
Enter nameBrooklynEnter
marks75
INSERTED
Do you want to continue?yEnter
rollno5
Enter nameshagun
Enter marks95
INSERTED
Do you want to continue?n1.Create
a database 2.Show all databases
3.Create a table
4.Insert the record5.Update
record 6.Delete record
7.Show the records 8.Exit
Enter your choice=5
Enter rollno to be updated2Enter
updated marks85
UPDATED
1.Create a database 2.Show
all databases3.Create a table
4.Insert the record 5.Update
record 6.Delete record
7.Show the records 8.Exit
Enter your choice=6
Enter rollno whose record to be deleted2
DELETED
1.Create a database 2.Show
all databases
3.Create a table 4.Insert the
record5.Update record
6.Delete record 7.Show the
records 8.Exit
Enter your choice=7 (1,
'brooklyn', 85.0)
(5, 'shagun', 95.0) 1.Create a
database 2.Show all
databases3.Create a table
4.Insert the record 5.Update
record 6.Delete record
7.Show the records 8.Exit
Enter your choice=8
Question 17:
Write a program to insert a record into the table using Python Interface.

Program:

import mysql.connector as sql conn=sql.connect(host='localhost',user='root',password=


'numerator',database='exam',auth_plugin="mysql_native_password")
cursor=conn.cursor()

print('Enter record into students table') n=int(input('How many records you want
to enter? '))

for i in range(n): roll=int(input('Enter rollno '))


name=input('Enter name ')
marks=float(input('Enter marks '))
a="insert into classxii values({},'{}',{})".format(roll,name,marks)
cursor.execute(a)
conn.commit()
print("----INSERTED --------------------- ")

OUTPUT:

Enter record into students table


How many records you want to enter? 2Enter rollno
101
Enter name KenjiEnter
marks 85
INSERTED
Enter rollno 102 Enter
name DariusEnter marks
72
INSERTED
Question 18:
Write a python program that displays first three rows fetched
from student table of Mysql database “School”. Use
mysql.connector to connect with the database.

Program:

import mysql.connector as sql conn=sql.connect(host='localhost',user='root',password=


'numerator',database='school',auth_plugin="mysql_native
_password")
cursor=conn.cursor()

a="select * from student"


cursor.execute(a)
n=int(input('How many record you want to see: '))
data=cursor.fetchmany(n)
for j in data:print(j)

OUTPUT:

How many record you want to see: 3(1,


'Brooklyn', Decimal('85.0'))
(2, 'Darius', Decimal('77.0'))
(3, 'Kenji', Decimal('72.0'))
Question 19:
Write a Python program to displays all the records from student table of
MySQL database “School”.

Program:
import mysql.connector as sql conn=sql.connect(host='localhost',user='root',password=
'numerator',database='school',auth_plugin="mysql_native
_password")
cursor=conn.cursor()

a="select * from student"


cursor.execute(a)

data=cursor.fetchall()for j in
data:
print(j)

OUTPUT:

(1, 'Brooklyn', Decimal('85.0'))


(2, 'Darius', Decimal('77.0'))
(3, 'Kenji', Decimal('72.0'))
(4, 'Sammy', Decimal('82.0'))
Question 20:
Perform all the operations like insert record, display record, update
record and delete record with reference to table ‘Employee’ having field
empno, ename, sal where empno is primary key, through MySQL-
Python connectivity.

Program:

import mysql.connector as sql mydb=sql.connect(host="localhost",user="root",password=


"numerator",database="school")
mycursor=mydb.cursor()
query1="create table if not exists emp1oyee(Empno int primary key,Ename
varchar(20),Sal decimal(7,2))" mycursor.execute(query1)
def insertrec(): mydb=sql.connect(host="localhost",user="root",passw
ord="numerator",database="school")
cursor=mydb.cursor()
while True:
eno=int(input('Enter eno')) name=input('Enter
name') salary=float(input('Enter salary'))
query1="insert into employee values({},'{}',{})
".format(eno,name,salary)
cursor.execute(query1)
mydb.commit()
print("----INSERTED --------------------- ")
ch=input('Do you want to continue?')if ch=='n' or
ch=='N':
break
cursor.close()
def displayrec(): mydb=sql.connect(host="localhost",user="root",passw
ord="numerator",database="school")
cursor=mydb.cursor() query2="select * from
employee"cursor.execute(query2)
data=cursor.fetchall()
for j in data:print(j)
cursor.close()def
updaterec():
mydb=sql.connect(host="localhost",user="root",passw
ord="numerator",database="school")
cursor=mydb.cursor()
r=int(input('Enter empno to be updated: ')) m=float(input('Enter updated
salary: ')) query3="update employee set Sal={} where Empno={}".
format(m,r)
cursor.execute(query3)mydb.commit()
print("---UPDATED ------------------ ")
cursor.close()def
deleterec():
mydb=sql.connect(host="localhost",user="root",passw
ord="numerator",database="school")
cursor=mydb.cursor()
r=input('Enter empno whose record to be deleted') query4="delete from employee
where Empno={}".format
(r,)
cursor.execute(query4)mydb.commit()
print("------DELETED ------------------------ ")
cursor.close()while
True:
print("1.Insert the record") print("2.Update
record") print("3.Delete record") print("4.Show the
records") print("5.Exit") ch=int(input("Enter your
choice="))if ch==1:
insertrec()elif
ch==2:
updaterec()elif
ch==3:
deleterec()elif
ch==4:
displayrec( )
. elif ch==5:
break
else:
print('Invalid choice')
Output

1.Insert the record2.Update


record 3.Delete record
4.Show the records 5.Exit
Enter your choice=1Enter
eno5
Enter nameSammy Enter
salary45000
INSERTED
Do you want to continue?n1.Insert
the record 2.Update record
3.Delete record 4.Show
the records5.Exit
Enter your choice=2
Enter empno to be updated: 2Enter
updated salary: 65000
UPDATED
1.Insert the record2.Update
record 3.Delete record
4.Show the records 5.Exit
Enter your choice=3
Enter empno whose record to be deleted5
DELETED
1. Insert the record
2.Update record 3.Delete
record 4.Show the records
5.Exit
Enter your choice=4
(1, 'Ben', Decimal('65000.00'))
(2, 'Brooklyn', Decimal('65000.00'))
(3, 'Darius', Decimal('45000.00'))
(4, 'Kenji', Decimal('65000.00'))1.Insert the
record
2. Update record 3.Delete
record 4.Show the records
5.Exit
Enter your choice=5

You might also like