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

Practical 10

This document discusses designing database applications in Python to create, search, and modify records in a database. It includes code samples to create a database, search records in a table, add new records, delete records, and update existing records. The code connects to a MySQL database called "Academy" and performs operations on a table called "student" which has fields for student ID and name. The output of running the code is not shown.
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)
40 views5 pages

Practical 10

This document discusses designing database applications in Python to create, search, and modify records in a database. It includes code samples to create a database, search records in a table, add new records, delete records, and update existing records. The code connects to a MySQL database called "Academy" and performs operations on a table called "student" which has fields for student ID and name. The output of running the code is not shown.
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

Kriti M.

Doongurse College Python Practical

10-a) Design the database applications for the following:


Aim: Design a simple database application that stores the records and retrieve the
same.
#Creating a database:
CODE:
import mysql.connector
mydb=mysql.connector.connect(host='localhost',user='root',password='12345678')
mycursor = mydb.cursor()
mycursor.execute("CREATE DATABASE Academy")
mycursor.execute("SHOW DATABASES")
for x in mycursor:
print(x)

OUTPUT:

Harsh.D.Thakur Roll No:-82


Kriti M.Doongurse College Python Practical

10-b) Design a database application to search the specified record from the
database.
CODE:
import mysql.connector
mydb = mysql.connector.connect(
host='localhost',
user='root',
password='12345678',
database='academy')
mycursor = mydb.cursor()
mycursor.execute("SELECT * FROM student")
myresult = mycursor.fetchall()
for x in myresult:
print(x)

OUTPUT:

Harsh.D.Thakur Roll No:-82


Kriti M.Doongurse College Python Practical

10-c) Design a database application to that allows the user to add, delete and
modify the records.
i) ADDING A RECORD TO EXISTING RECORDS:
CODE:
import mysql.connector
mydb = mysql.connector.connect(
host='localhost',
user='root',
password='12345678',
database='academy')
mycursor = mydb.cursor()
a ="INSERT INTO student (student_id, student_name) VALUES (%s, %s)"
b = [("7", "Ramesh")]
mycursor.executemany(a, b)
mydb.commit()
print(mycursor.rowcount, "was inserted.")

OUTPUT:

Harsh.D.Thakur Roll No:-82


Kriti M.Doongurse College Python Practical

ii) DELETING A RECORD FROM EXISTING RECORDS:


CODE:
import mysql.connector
mydb = mysql.connector.connect(
host='localhost',
user='root',
password='12345678',
database='academy')
mycursor = mydb.cursor()
a ="DELETE FROM student WHERE student_name = 'Ramesh'"
mycursor.execute(a)
mydb.commit()
print(mycursor.rowcount, "record(s) deleted")

OUTPUT:

Harsh.D.Thakur Roll No:-82


Kriti M.Doongurse College Python Practical

iii) UPDATING A RECORD FROM EXISTING RECORD:


CODE:
import mysql.connector
mydb = mysql.connector.connect(
host='localhost',
user='root',
password='12345678',
database='academy')
mycursor = mydb.cursor()
a ="UPDATE student SET student_name='Sachin' WHERE
student_name='Rahul'"
mycursor.execute(a)
mydb.commit()
print(mycursor.rowcount, "record(s) affected")

OUTPUT:

Harsh.D.Thakur Roll No:-82

You might also like