0% found this document useful (0 votes)
4 views6 pages

MySQL and Python: Database Operations

The document provides a step-by-step guide on connecting Python to a MySQL database, creating tables, inserting data, updating records, and deleting records. It includes code snippets for each operation, demonstrating how to manage a 'STUDENT' and 'COURSE' table within a database named 'akshansh'. The document emphasizes the use of the mysql.connector library for database interactions.

Uploaded by

omkarpal2276
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)
4 views6 pages

MySQL and Python: Database Operations

The document provides a step-by-step guide on connecting Python to a MySQL database, creating tables, inserting data, updating records, and deleting records. It includes code snippets for each operation, demonstrating how to manage a 'STUDENT' and 'COURSE' table within a database named 'akshansh'. The document emphasizes the use of the mysql.connector library for database interactions.

Uploaded by

omkarpal2276
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

CONNECTING MY SQL AND

PYTHON
import [Link]

mydb = [Link](
host="localhost",
user="root",
password="doon2123",
database="akshansh"
)
print("Connection Successful")
[Link]()
CREATING TABLE
import [Link]
mydb = [Link](
host='localhost',
user='root',
passwd='doon2123',
database='akshansh'
)
mycursor = [Link]()
[Link]("""
CREATE TABLE STUDENT(
Adm_no INT(10), S_Name CHAR(30),
Gender CHAR(1), DOB DATE,
Stream CHAR(15), Marks FLOAT(4,2)
)
""")
[Link]("""
CREATE TABLE COURSE( Course_id INT, C_name CHAR(30), Duration INT)""")
[Link]("SHOW TABLES")
for x in mycursor:
print(x)
INSERTING DATA INTO TABLE
import [Link]
mydb = [Link](
host="localhost",
user="root",
passwd="doon2123",
database="akshansh"
)
mycursor = [Link]()
[Link](
"INSERT INTO student VALUES (%s, %s, %s, %s, %s, %s)",
(2201, 'Mohit', 'M', '2005-01-28', 'Science', 95.2)
)
[Link](
"INSERT INTO student VALUES (%s, %s, %s, %s, %s, %s)",
(2202, 'Raghav', 'M', '2004-10-04', 'Commerce', 93.25)
)
[Link](
"INSERT INTO student VALUES (%s, %s, %s, %s, %s, %s)",
(2203, 'Tanuja', 'F', '2003-10-04', 'Arts', 91.8)
)
[Link]('SELECT * FROM student')
mydata = [Link]()
nrec = [Link]
print("Total records fetched so far are:", nrec)
for i in mydata:
print(i)
[Link]()
UPDATING DATA
import [Link]
mydb=[Link](
host='localhost',
user='root',
passwd='doon2123',
database='akshansh')
mycursor=[Link]()
[Link]("update student set stream='Commerce' where admn_no=2205")
[Link]("update student set stream='Arts' where admn_no=2204")
[Link]('select*from student')
mydata=[Link]()
print('Records after updation are:')
for i in mydata:
print(i)
[Link]()
USING DELETE COMMAND
import [Link]
mydb=[Link](
host='localhost',
user='root',
passwd='doon2123',
database='akshansh')
mycursor=[Link]()
[Link]('DELETE FROM STUDENT WHERE MARKS=91.8')
[Link]()
print([Link],'RECORD DELETED')
[Link]()

You might also like