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

Python MySQL Connectivity 25th March 2025

The document contains notes by Anand Sir for CBSE Computer Science Python classes 11 and 12, featuring various MySQL database operations such as connecting to a database, inserting, updating, deleting, and fetching data. It includes Python code examples for each operation, demonstrating how to interact with a MySQL database using the mysql.connector library. The notes are intended for educational purposes and direct readers to the CODEITUP YouTube channel and website for further resources.

Uploaded by

samzzyy03
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)
15 views

Python MySQL Connectivity 25th March 2025

The document contains notes by Anand Sir for CBSE Computer Science Python classes 11 and 12, featuring various MySQL database operations such as connecting to a database, inserting, updating, deleting, and fetching data. It includes Python code examples for each operation, demonstrating how to interact with a MySQL database using the mysql.connector library. The notes are intended for educational purposes and direct readers to the CODEITUP YouTube channel and website for further resources.

Uploaded by

samzzyy03
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/ 14

Notes By: Anand Sir, YouTube Channel: CODEITUP

CODEITUP

Notes By: Anand Sir


Author: CBSE CS Python Class 11 & 12
YouTube Channel: CODEITUP
For CS Class 11 & 12 Book, Courses & Projects Visit: https://codeitup.in
codeitup @codeitupyt codeituplearners codeitupyt

Notes By: Anand Sir, YouTube Channel: CODEITUP

Notes By: Anand Sir


Author: CBSE CS Python Class 11 & 12
YouTube Channel: CODEITUP
For CS Class 11 & 12 Book, Courses & Projects Visit: https://codeitup.in
codeitup @codeitupyt codeituplearners codeitupyt

Notes By: Anand Sir, YouTube Channel: CODEITUP

Notes By: Anand Sir


Author: CBSE CS Python Class 11 & 12
YouTube Channel: CODEITUP
For CS Class 11 & 12 Book, Courses & Projects Visit: https://codeitup.in
codeitup @codeitupyt codeituplearners codeitupyt

Notes By: Anand Sir, YouTube Channel: CODEITUP

Notes By: Anand Sir


Author: CBSE CS Python Class 11 & 12
YouTube Channel: CODEITUP
For CS Class 11 & 12 Book, Courses & Projects Visit: https://codeitup.in
codeitup @codeitupyt codeituplearners codeitupyt

Notes By: Anand Sir, YouTube Channel: CODEITUP

Notes By: Anand Sir


Author: CBSE CS Python Class 11 & 12
YouTube Channel: CODEITUP
For CS Class 11 & 12 Book, Courses & Projects Visit: https://codeitup.in
codeitup @codeitupyt codeituplearners codeitupyt

Notes By: Anand Sir, YouTube Channel: CODEITUP

Notes By: Anand Sir


Author: CBSE CS Python Class 11 & 12
YouTube Channel: CODEITUP
For CS Class 11 & 12 Book, Courses & Projects Visit: https://codeitup.in
codeitup @codeitupyt codeituplearners codeitupyt

Notes By: Anand Sir, YouTube Channel: CODEITUP


#Program to Check Python MySQL Connetion.
import mysql.connector as c
con=c.connect(host='localhost',
user='root',
password='123456',
database='live25')
if con.is_connected():
print("Successfully Connected...")

Notes By: Anand Sir


Author: CBSE CS Python Class 11 & 12
YouTube Channel: CODEITUP
For CS Class 11 & 12 Book, Courses & Projects Visit: https://codeitup.in
codeitup @codeitupyt codeituplearners codeitupyt

Notes By: Anand Sir, YouTube Channel: CODEITUP


#Inserting data in the table.
import mysql.connector as c
con=c.connect(host="localhost",
user="root",
passwd="123456",
database="live25")
cursor=con.cursor()
while True:
roll=int(input("Enter Roll:"))
name=input("Enter Student Name:")
marks=int(input("Enter Marks:"))
query="Insert into student values({},'{}',{})".format(roll,name,marks)
cursor.execute(query)
con.commit()
print("Data Inserted Successfully..")
choice=int(input("1->Enter More\n2->Exit\nEnter Your Choice:"))
if choice==2:
break

Notes By: Anand Sir


Author: CBSE CS Python Class 11 & 12
YouTube Channel: CODEITUP
For CS Class 11 & 12 Book, Courses & Projects Visit: https://codeitup.in
codeitup @codeitupyt codeituplearners codeitupyt

Notes By: Anand Sir, YouTube Channel: CODEITUP


#Update table data
import mysql.connector as c
con=c.connect(host="localhost",
user="root",
passwd="123456",
database="live25")
cursor=con.cursor()
rno=int(input("Enter Roll Number:"))
newmarks=int(input("Enter Marks(Updated):"))
query="update student set marks={} where roll={}".format(newmarks,rno)
cursor.execute(query)
con.commit()
if cursor.rowcount>0:
print("Data Updated Successfully...")
else:
print("No Data Found.")

Notes By: Anand Sir


Author: CBSE CS Python Class 11 & 12
YouTube Channel: CODEITUP
For CS Class 11 & 12 Book, Courses & Projects Visit: https://codeitup.in
codeitup @codeitupyt codeituplearners codeitupyt

Notes By: Anand Sir, YouTube Channel: CODEITUP


#Deleting Data
import mysql.connector as c
con=c.connect(host="localhost",
user="root",
passwd="123456",
database="live25")
cursor=con.cursor()
rno=int(input("Enter Roll Number:"))
query="delete from student where roll={}".format(rno)
cursor.execute(query)
con.commit()
if cursor.rowcount>0:
print("Deletion Successfull..")
else:
print("Roll Number not Found..")

Notes By: Anand Sir


Author: CBSE CS Python Class 11 & 12
YouTube Channel: CODEITUP
For CS Class 11 & 12 Book, Courses & Projects Visit: https://codeitup.in
codeitup @codeitupyt codeituplearners codeitupyt

Notes By: Anand Sir, YouTube Channel: CODEITUP


#Fetching All data from Table
import mysql.connector as c
con=c.connect(host="localhost",
user="root",
passwd="123456",
database="live25")
cursor=con.cursor()
query="select * from student"
cursor.execute(query)
record=cursor.fetchall()
print(record)

Notes By: Anand Sir


Author: CBSE CS Python Class 11 & 12
YouTube Channel: CODEITUP
For CS Class 11 & 12 Book, Courses & Projects Visit: https://codeitup.in
codeitup @codeitupyt codeituplearners codeitupyt

Notes By: Anand Sir, YouTube Channel: CODEITUP


#Fetching Conditional Data
import mysql.connector as c
con=c.connect(host="localhost",
user="root",
passwd="123456",
database="live25")
cursor=con.cursor()
rollnum=int(input("Enter Roll Number to Search:"))
query="select * from student where roll={}".format(rollnum)
cursor.execute(query)
record=cursor.fetchone()
if cursor.rowcount>0:
print(record)
else:
print("Invalid Roll Number")

Notes By: Anand Sir


Author: CBSE CS Python Class 11 & 12
YouTube Channel: CODEITUP
For CS Class 11 & 12 Book, Courses & Projects Visit: https://codeitup.in
codeitup @codeitupyt codeituplearners codeitupyt

Notes By: Anand Sir, YouTube Channel: CODEITUP


#Searching a record
import mysql.connector as c
con=c.connect(host="localhost",
user="root",
passwd="123456",
database="live25")
cursor=con.cursor()
query="select * from student"
cursor.execute(query)
record=cursor.fetchall()
for i in record:
print(i)
rno=int(input("Enter Roll Number to Search:"))
flag=0
for i in record:
if i[0]==rno:
print(i)
flag=1
break
if flag==0: Notes By: Anand Sir
print("Roll No Not Found...")
Author: CBSE CS Python Class 11 & 12
YouTube Channel: CODEITUP
For CS Class 11 & 12 Book, Courses & Projects Visit: https://codeitup.in
codeitup @codeitupyt codeituplearners codeitupyt

Notes By: Anand Sir, YouTube Channel: CODEITUP

#Inserting using %s concept


import mysql.connector as c
con=c.connect(host="localhost",
user="root",
passwd="123456",
database="live25")
cursor=con.cursor()
roll=int(input("Enter Roll:"))
name=input("Enter Student Name:")
marks=int(input("Enter Marks:"))
data=(roll,name,marks)
query="Insert into student values(%s,%s,%s)"
cursor.execute(query,data)
con.commit()
print("Data Inserted Successfully..")
Notes By: Anand Sir
Author: CBSE CS Python Class 11 & 12
YouTube Channel: CODEITUP
For CS Class 11 & 12 Book, Courses & Projects Visit: https://codeitup.in

You might also like