0% found this document useful (0 votes)
27 views5 pages

Database Connectivity Program

Uploaded by

rudrakshgoyal112
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)
27 views5 pages

Database Connectivity Program

Uploaded by

rudrakshgoyal112
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/ 5

import mysql.

connector
mycon=mysql.connector.connect(host="localhost",user="root",passwd="root123")
if mycon.is_connected():
print("Connection Established")
else:
print("Error in connection")
c1=mycon.cursor()
c1.execute("create database if not exists learn")
c1.execute("use learn")

def tablecreate():
c1=mycon.cursor() # Cursor created

c1.execute("create table if not exists student\


(\
rollno int(3) primary key,\
name varchar(20),\
class varchar(5),\
sec char(1), marks integer(3))")
print("Table created")

def insertrecord():
c1=mycon.cursor()
# single row values at run time
r=int(input("enter rollno:"))
n=input("enter name:")
c=input("enter class:")
s=input("enter section:")
m=int(input("enter marks:"))

Page 1.1
m=int(input("enter marks:"))
sq="insert into student(rollno,name,class,sec,marks)values(%s,%s,%s,%s,%s)"
a=(r,n,c,s,m)
c1.execute(sq,a)
mycon.commit()
print("Row inserted")

def updaterecord():
c1=mycon.cursor()
r=int(input("enter roll no:")) #updation of values at run time
print("What you want to update?")
print("1. Name of Student")
print("2. Class of Student")
print("3. Section of Student")
print("4. Marks of Student")
ch=int(input("Enter Your Choice:"))
if ch==1:
na=input("enter Name to be updated:")
k=(na,r)
q="update student set name=%s where rollno=%s"
c1.execute(q,k)
mycon.commit()
print("Name Updated")
elif ch==2:
cl=input("Enter Class to be updated:")
k=(cl,r)
q="update student set class=%s where rollno=%s"
c1.execute(q,k)
mycon.commit()
print("Class Updated")
elif ch==3:
sec=input("Enter Section to be updated:")
k=(sec,r)

Page 1.2
k=(sec,r)
q="update student set sec=%s where rollno=%s"
c1.execute(q,k)
mycon.commit()
print("Section Updated")
elif ch==4:
m=input("Enter Marks to be updated:")
k=(m,r)
q="update student set marks=%s where rollno=%s"
c1.execute(q,k)
mycon.commit()
print("Marks Updated")

def deleterecord():
c1=mycon.cursor()
r=int(input("enter roll no:"))
a=(r,)
q="Delete from student where rollno=%s"
c1.execute(q,a)
mycon.commit()
print("Record Deleted")

def readrecord():
c1=mycon.cursor()
print("What you want to retreive?")
print("1. Single Record")
print("2. Specific No of Rows")
print("3. Whole Table")
print("4. Specific records based on Condition")
ch=int(input("Enter Your Choice"))
if ch==1:
s="Select * from Student"
c1.execute(s)

Page 1.3
c1.execute(s)
r=c1.fetchone()
print(r)
elif ch==2:
s=int(input("How many rows You want to display?:"))
q="Select * from student"
c1.execute(q)
r=c1.fetchmany(s)
for i in r:
print(i)
elif ch==3:
q="Select * from student"
c1.execute(q)
r=c1.fetchall()
for i in r:
print(i)
elif ch==4:
c=input("Enter Class in Romans:")
q="Select * from student where class=%s";
a=(c,)
c1.execute(q,a);
r=c1.fetchall()
print("========================================")
for x in r:
print(x)

def main_menu():
print("Database Operations")
print("====================")
print("1. Table Creation");
print("2. Insert Records");

Page 1.4
print("2. Insert Records");
print("3. Update Records");
print("4. Delete Records");
print("5. Show Records");
print("6. Exit")
ch=int(input("Enter Your Choice:"))
if ch==1:
tablecreate()
elif ch==2:
insertrecord()
elif ch==3:
updaterecord()
elif ch==4:
deleterecord()
elif ch==5:
readrecord()
elif ch==6:
exit()

main_menu()

Page 1.5

You might also like