0% found this document useful (0 votes)
28 views6 pages

Project Work

Uploaded by

rihanais12345
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)
28 views6 pages

Project Work

Uploaded by

rihanais12345
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/ 6

Project- Python SQL connectivity

Q) Write a python programme to establish connectivity between SQL and Python and retrieve records
from student table and apply the following methods.
1. Create database
2. Create table
3. Add records
4. Display data
5. Search by roll no
6. Search by name
7. Delete records
8. Update records

import mysql.connector

#1 create database
def createdatabase():
mydb=mysql.connector.connect(host='localhost', user='root', passwd='adisg')
mycursor=mydb.cursor()
mycursor.execute('create database if not exists class12E2024')
mycursor.execute('show databases')
for i in mycursor:
print(i)

#2 create table
def createtable():
mydb=mysql.connector.connect(host='localhost', user='root', passwd='adisg', database='class12E2024')
mycursor=mydb.cursor()
myrecords=mycursor.execute('Create table student12 (Rollno int(3) primary key, Name varchar(20), Age
int(2), Marks int(3))')
mycursor.execute('desc student12')
for i in mycursor:
print(i)

#3 adding records
def addrec():
mydb=mysql.connector.connect(host='localhost', user='root', passwd='adisg', database='class12E2024')
mycursor=mydb.cursor()
n=int(input('Enter no: of records to be added:'))
try:
for i in range(n):
rno=int(input('Enter roll no:'))
rname=input('Enter name:')
age=int(input('Enter age:'))
marks=int(input('Enter marks:'))
q1="Insert into student12 values({}, '{}', {},{})".format(rno, rname, age ,marks)
mycursor.execute(q1)
mydb.commit()
print('**record added successfully**')
except Exception as e:
print('**record not added successfully**', e)

#4 display data
def fetchdata():
try:
mydb=mysql.connector.connect(host='localhost', user='root', passwd='adisg', database='class12E2024')
mycursor=mydb.cursor()
mycursor.execute('Select * from student12')
myrecords=mycursor.fetchall()
print('%7s '%'Rollno','%10s'%'Name', '%7s '%'Age', '%7s '%'Marks')
for x in myrecords:
print('%7s '%x[0],'%10s'%x[1], '%7s '%x[2], '%7s '%x[3])
except Exception as e:
print('Unable to display\n', e)

#5 search by rollno
def searchbyrollno():
try:
mydb=mysql.connector.connect(host='localhost', user='root', passwd='adisg', database='class12E2024')
mycursor=mydb.cursor()
reqrno=int(input('Enter the roll no:'))
quer='Select * from student12 where Rollno={}', format(reqrno)
mycursor.execute(query)
myrecords=mycursor.fetchall()
if mycursor.rowcount<=0:
print('\##SORRY! NO MATCHING DETAILS AVAILABLE##')
else:
print('%7s '%'Roll no','%10s'%'Name', '%7s '%'Age', '%7s '%'Marks')
for x in myrecords:
print('%7s '%x[0],'%10s'%x[1], '%7s '%x[2], '%7s '%x[3])
print('Search successful')
except:
print('Record not found')

#6 search by name
def searchbyname():
try:
mydb=mysql.connector.connect(host='localhost', user='root', passwd='adisg', database='class12E2024')
mycursor=mydb.cursor()
reqrname=input('Enter the name:')
query="Select * from student12 where Name='{}'".format(reqrname)
mycursor.execute(query)
myrecords=mycursor.fetchall()
if mycursor.rowcount<=0:
print('\##SORRY! NO MATCHING DETAILS AVAILABLE##')
else:
print('%7s '%'Rollno','%10s'%'Name', '%7s '%'Age', '%7s '%'Marks')
for x in myrecords:
print('%7s '%x[0],'%10s'%x[1], '%7s '%x[2], '%7s '%x[3])
print('Search successful')
except Error as e:
print(e)

#7 delete record
def deletebyrno():
try:
mydb=mysql.connector.connect(host='localhost', user='root', passwd='adisg', database='class12E2024')
mycursor=mydb.cursor()
reqrno=int(input('Enter the roll no: you want to delete:'))
query='Select * from student12 where Rollno='+str(reqrno)
mycursor.execute(query)
results=mycursor.fetchall()
if mycursor.rowcount<=0:
print('\##SORRY! NO MATCHING DETAILS AVAILABLE##')
else:
print('%7s '%'Rollno','%10s'%'Name', '%7s '%'Age', '%7s '%'Marks')
for row in results:
print('%7s '%row[0],'%10s'%row[1], '%7s '%row[2], '%7s '%row[3])
print()
ans=input('Are you sure to delete? (y/n)')
if ans=='y' or ans=='Y':
query='Delete from student12 where Rollno={}'.format(reqrno)
mycursor.execute(query)
mydb.commit()
print('\n## RECORD DELETED ##')
else:
print('Not deleted')
except Error as e:
print(e)

#8 Update record
def Editbyrno():
try:
mydb=mysql.connector.connect(host='localhost', user='root', passwd='adisg', database='class12E2024')
mycursor=mydb.cursor()
reqrno=input('Enter the roll number:')
query='Select * from student12 where Rollno='+str(reqrno)
mycursor.execute(query)
results=mycursor.fetchall()
if mycursor.rowcount<=0:
print('\##SORRY! NO MATCHING DETAILS AVAILABLE##')
else:
nm=input('Enter new name to update (Enter old value if not to update):')
mk=int(input('Enter new marks to update (Enter old value if no to update):'))
query="Update student12 set Name='{}', Marks={} where Rollno={}".format(nm,mk,reqrno)
mycursor.execute(query)
mydb.commit()
print('\n## RECORD UPDATED ##')
fetchdata()
except Error as e:
print(e)

def menu():
ans='y'
while ans=='y' or ans=='Y':
print('1. Create database')
print('2. Create table')
print('3. Add records')
print('4. Display data')
print('5. Search by roll no')
print('6. Search by name')
print('7. Delete records')
print('8. Update records')

print()
ch=int(input('Enter choice:'))
print()
if ch==1:
createdatabase()
elif ch==2:
createtable()
elif ch==3:
addrec()
elif ch==4:
fetchdata()
elif ch==5:
searchbyrollno()
elif ch==6:
searchbyname()
elif ch==7:
deletebyrno()
elif ch==8:
Editbyrno()
else:
break
print()
ans=input('Do you want to continue ?')
menu()

OUTPUT:

1. Create database
2. Create table
3. Add records
4. Display data
5. Search by roll no
6. Search by name
7. Delete records
8. Update records

Enter choice:1

('information_schema',)
('12a',)
('advika2024',)
('class11ip2023',)
('class12a2023',)
('class12e2024',)
('class12ip2024',)
('class1ip2023',)
('csc_exam_22_23',)
('cscclass2023',)
('fathima_11g',)
('ip_exam_23',)
('ippracticals',)
('mysql',)
('practical_exam_2023',)
('practicalexam',)
('test',)

Do you want to continue ?y


Enter choice:3

Enter no: of records to be added:1


Enter roll no:3
Enter name:je
Enter age:17
Enter marks:89
**record added successfully**

Do you want to continue ?y


Enter choice:4

Rollno Name Age Marks


1 cd 17 90
2 diya 18 92
3 je 17 89

Do you want to continue ?y


Enter choice:5

Enter the roll no:1


Rollno Name Age Marks
1 cd 17 90
Search successful

Do you want to continue ?y


Enter choice:6

Enter the name:cd


Rollno Name Age Marks
1 cd 17 90
Search successful

Do you want to continue ?y


Enter choice:7

Enter the roll no: you want to delete:2


Rollno Name Age Marks
2 diya 18 92

Are you sure to delete? (y/n)y

## RECORD DELETED ##

Do you want to continue ?y


Enter choice:8

Enter the roll number:3


Enter new name to update (Enter old value if not to update):joann
Enter new marks to update (Enter old value if no to update):90

## RECORD UPDATED ##
Rollno Name Age Marks
1 cd 17 90
3 joann 17 90

Result- program executed successfully

You might also like