0% found this document useful (0 votes)
0 views7 pages

Python Exp 6

Uploaded by

BADASS GAMING YT
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)
0 views7 pages

Python Exp 6

Uploaded by

BADASS GAMING YT
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/ 7

Experiment : 06

Title : Program to demonstrate CRUD (create, read, update and delete) operations on
database using python.

Program:

Aim : Program To create database


import pymysql
#step1: Established connection with database
mycon=pymysql.connect(host='localhost',user='root',passwd='')
print(mycon)
#step2:Create a Cursur(place Where SQL queries are written in the memory area
mycur=mycon.cursor()
#step3:Execute the sql queries
mycur.execute("create database pythondb")
print('----Database crated----')
#step4: close the connection
mycon.close()

Aim : Program to create table


import pymysql
mycon=pymysql.connect(host='localhost',user='root',password='',
database='pythondb')
mycur=mycon.cursor()
mycur.execute('create table student(rollno int primary key,name
varchar(20),marks float,dob date)')
print('table created...')
mycon.close()

Aim : Program To insert values in the table


import pymysql
mycon=pymysql.connect(host='localhost',user='root',password='',
database='pythondb')
mycur=mycon.cursor()
mycur.execute("insert into student
values(101,'Prashant',90,'1991-12-23')")
mycur.execute("insert into student
values(102,'Shivendra',100,'1984-10-03')")
mycur.execute("insert into student
values(103,'Ayush',45,'1992-09-10')")
mycur.execute("insert into student
values(104,'Nilesh',60,'1990-01-15')")
mycur.execute("insert into student
values(105,'Anand',80,'1991-05-19')")
print('---Record inserted__')
mycon.commit()
mycon.close()

Aim : Program to update values in the table


import pymysql
mycon=pymysql.connect(host='localhost',user='root',password='',
database='pythondb')
mycur=mycon.cursor()
mycur.execute("update student set marks=100 where rollno=101")
print('Record updated.....')
mycon.commit()
mycon.close()
Aim : Program to delete values from the table
import pymysql
mycon=pymysql.connect(host='localhost',user='root',password='',
database='pythondb')
mycur=mycon.cursor()
mycur.execute("delete from student where rollno=105")
print('Record delete.....')
mycon.commit()
mycon.close()
Aim : Program to take user input .
import pymysql
mycon=pymysql.connect(host='localhost',user='root',password='',
database='pythondb')
mycur=mycon.cursor()
vrollno=input('Enter the rollno')
vname=input('Enter the name')
vmarks=input('Enter the marks')
vdob=input('Enter the date of birth')
mycur.execute("insert into studentvalues ('"+vrollno+"'
,'"+vname+"' ,'"+vmarks+"
','"+vdob+"' )")
print('Record inserted....')
mycon.commit()
mycon.close()
Final Output:
Conclusion: In this experiment we learned to demonstrate CRUD (create, read, update and
delete) operations on database using python.

You might also like