Extracted Pages
Extracted Pages
Integrate MySQL with Python by importing the MySQL module and add records of student
and display all the record.
CODE:
import mysql.connector
data=input("Enter name of the database")
mydata=mysql.connector.connect(host='localhost', user='root', password='P@ssw0rd', database='bishwashri')
mycursor=mydata.cursor()
'''
sql="create database if not exists %s"%(data)
mycursor.execute(sql)
print("Database created successfully")
mycursor.execute('USE '+data)
tabname=input("Enter name of the table")
query="create table if not exists "+tabname+" \
(studentid int primary key,\
name varchar(255)not null,\
class char(20),\
section char(26))"
mycursor.execute(query)
print("table "+tabname+" is created successfully")
mydata.close()
'''
std=int(input("Enter student id:"))
sname=input("Enter student name:")
cls=input("Enter class:")
sec=input("Enter section:")
Query="Insert into students values({},'{}','{}','{}')".format(std,sname,cls,sec)
mycursor.execute(Query)
mydata.commit()
40
PROGRAM #20
Integrate MySQL with Python by importing the MySQL module to search student using roll
number, name, age, class and if present in table display the record, if not display appropriate
method.
CODE:
import mysql.connector
d=input("Enter name of the database")
myd=mysql.connector.connect(host='localhost', user='root', password=‘P@ssw0rd', database='bishwashri')
mycursor=myd.cursor()
def Search_student_s_Details():
42
PROGRAM #21
Integrate SQL with Python by importing the MySQL module to search a student using rollno,
delete the record.
CODE:
import mysql.connector
d=input("Enter name of the database")
mycon=mysql.connector.connect(host='localhost', user='root', password='P@ssw0rd',database='bishwashri')
MyCur=mycon.cursor()
def Delete_student_s_Details():
mycon=mysql.connector.connect(user='root',password=‘P@ssw0rd',host='localhost',database='bishwashri')
temp_rollno=input("Enter Roll Number to be Deleted:")
MyCur=mycon.cursor()
Query= "Delete from student_s where rollno= '%s' "%(temp_rollno)
'''rec_srch=(temp_rollno)
'''
MyCur.execute(Query)
mycon.commit()
MyCur.close()
mycon.close()
Delete_student_s_Details()
OUTPUT:
44
PROGRAM #22
Integrate SQL with Python by importing the MySQL module to search a student using rollno,
update the record.
CODE:
import mysql.connector
d=input("Enter name of the database")
mycon=mysql.connector.connect(host='localhost', user='root', password=‘P@ssw0rd',database='bishwashri')
MyCur=mycon.cursor()
def Update_student_s_Details():
mycon=mysql.connector.connect(user='root',password=‘P@ssw0rd',host='localhost',database='bishwashri')
MyCur=mycon.cursor()
temp_rollno=input("Enter Roll Number to be Updated:")
query="Select * from student_s where rollno= '%s'"%(temp_rollno)
MyCur.execute(query)
d=MyCur.fetchall()
if d!=None:
print("Old Records found:")
print(d)
print("Input for New data")
name=input("Enter Student Name :")
age=int(input("Enter Age :"))
Cl=input("Enter Class")
Q="Update student_s set name='%s',age='%d',Cl='%s' where rollno='%s' "%(name,age,Cl,temp_rollno)
MyCur.execute(Q)
mycon.commit()
print("Record has been updated Now")
else:
print("Sorry No such records found:")
myCur.close()
mycon.close()
Update_student_s_Details()
OUTPUT:
45