0% found this document useful (0 votes)
5 views

Extracted Pages

The document shows code to integrate MySQL with Python by importing the MySQL module. It demonstrates how to create a database and table, insert records, search, update and delete student records from the table.

Uploaded by

debosmitdas14798
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views

Extracted Pages

The document shows code to integrate MySQL with Python by importing the MySQL module. It demonstrates how to create a database and table, insert records, search, update and delete student records from the table.

Uploaded by

debosmitdas14798
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

PROGRAM #19

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()

print("Record has been saved.. in Students Data Table....")


'''
for(std,sname,cls,sec) in mycursor:
print("Student id", std)
print('Student name', sname)
print('Class', cls)
print('\n')
'''
Q=("select*from students")
mycursor.execute(Q)
for r in mycursor:

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()

sql="create database if not exists %s"%(d)


mycursor.execute(sql)
print("Database created successfully")
mycursor.execute('USE '+d)
tabname=input("Enter name of the table")
query="create table if not exists "+tabname+" \
(rollno int primary key,\
name varchar(255)not null,\
age int(20),\
Cl char(26))"
mycursor.execute(query)
print("table "+tabname+" is created successfully")
myd.close()

roll=int(input("Enter student rollno.:"))


sname=input("Enter student name:")
age=int(input("Enter age:"))
Cl=input("Enter class:")
Query="Insert into students values({},'{}',{},'{}')".format(roll,sname,age,Cl)
mycursor.execute(Query)
myd.commit()

print("Record has been saved.. in student2 Data Table....")


for(roll,sname,age,Cl) in mycursor:
print("Student rollno.", std)
print('Student name', sname)
print('Age', age)
print('Class', Cl)
print('\n')

def Search_student_s_Details():

temp_rollno=int(input("Enter roll Number to be Search :"))

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()

print("Record has been deleted Now ")

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

You might also like