0% found this document useful (0 votes)
7 views36 pages

Check MySQL Connector in Python

The document provides instructions for managing a MySQL database using Python, including checking for the MySQL connector installation, uninstalling and installing it, and performing CRUD operations such as fetching, updating, inserting, and deleting records from an employee table. It includes code snippets demonstrating how to connect to the database, execute queries, and handle results. The document emphasizes the importance of committing changes when updating or inserting records.

Uploaded by

Sunil Singh
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
7 views36 pages

Check MySQL Connector in Python

The document provides instructions for managing a MySQL database using Python, including checking for the MySQL connector installation, uninstalling and installing it, and performing CRUD operations such as fetching, updating, inserting, and deleting records from an employee table. It includes code snippets demonstrating how to connect to the database, execute queries, and handle results. The document emphasizes the importance of committing changes when updating or inserting records.

Uploaded by

Sunil Singh
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd

COMMAND TO CHECK MYSQL

CONNECTOR IS INSTALLED OR
NOT
IN PYTHON IDLE
UNINSTALL MYSQL CONNECTOR
IF ALREADY INSTALLED

pip uninstall mysql-connector-python


INSTALL MYSQL CONNECTOR
pip install mysql-connector-python
Fetchone
import [Link] as c
con=[Link](host="localhost",
username="root",
password="linus@321", Fetchone
database="carshowroom"
)
cur=[Link]()
query="select empid, empname, gender, designation from employee"
[Link](query)
row1=[Link]() # fetch one record/row
print(row1)
row2=[Link]() # fetch one record/row
print(row2)
row3=[Link]() # fetch one record/row
print(row3)
row=[Link](7) # fetch 7 more records/rows
print(row)
row11=[Link]() # fetch one record/row
print(row11)
row12=[Link]() # fetch None as now more records fetch
print(row12)
[Link]()
Fetchmany
import [Link] as c
con=[Link](host="localhost",
username="root", Fetchmany
password="linus@321",
database="carshowroom"
)
cur=[Link]()
query="select empid, empname, gender, salary from employee"
[Link](query)
row=[Link]() # fetch only 1 record if no value given in parameter
print(row)
row=[Link]() # fetch only 1 record if no value given in parameter
print(row)
row=[Link](9) # fetch remaining 9 records
print(row)
row=[Link]()
print(row) # Now more record to fetch therefore fetch
empty list []
[Link]()
Fetchall
import [Link] as c
con=[Link](host="localhost", Fetchall
username="root",
password="linus@321",
database="carshowroom"
)
cur=[Link]()
query="select empid, empname, gender, salary from
employee"
[Link](query)
row=[Link]()
print(row) # fetch all record in the form of List of Tuples
row=[Link]()
print(row) # Now more record to fetch therefore fetch empty
list []
import [Link] as c
con=[Link](host="localhost",
username="root",
password="linus@321",
database="carshowroom"
)
cur=[Link]()
g=input("Enter gender (F/M):")
s=int(input("Enter salary >:"))
query="select empid, empname,gender,designation,salary from
\
employee where gender='%s' and salary>=%s"%(g,s)
[Link](query)
data=[Link]()
for i in data:
import [Link] as c
con=[Link](host="localhost",
username="root",
password="linus@321",
database="carshowroom"
)
cur=[Link]()
g=input("Enter gender (F/M):")
s=int(input("Enter salary >=:"))
query="select empid, empname,gender,designation,salary
from \
employee where gender='{}' and salary>={}".format(g,s)
[Link](query)
data=[Link]()
for i in data:
update
Without
commit()
import [Link] as c
con=[Link](host="localhost",
username="root", Update
password="linus@321", without
database="carshowroom"commit()
)
cur=[Link]()
query="update employee set empname='Golu'\
where empid='E008’”
[Link](query)
#[Link]()
print("Data updated successfully.....")
[Link]()
import [Link] as c
con=[Link](host="localhost",
username="root",
Update with
password="linus@321", commit()
database="carshowroom"
)
cur=[Link]()
query="update employee set empname='Golu’\
where empid='E008’”
[Link](query)
[Link]()
print("Data updated successfully.....")
[Link]()
Insert new row
import [Link] as c
con=[Link](host="localhost",
username="root",
password="linus@321",
Insert new row
database="carshowroom"
)
cur=[Link]()
empid=input("Enter New Employee ID:")
empname=input("Enter New Employee Name:")
gender=input("Enter New Employee Gender(M/F):")
dob=input("Enter Employee Date of Birth:(YYYY-MM-DD):")
doj=input("Enter Employee Date of Joining:(YYYY-MM-DD):")
d=input("Enter Empolyee Designation:")
s=int(input("Enter Employee Salary:"))
query="insert into employee values ('%s','%s','%s','%s','%s','%s',%s)\
"%(empid,empname,gender,dob,doj,d,s)
[Link](query)
[Link]()
print("Data inserted successfully.....")
[Link]()
Insert
multiple new
rows
import [Link] as c
con=[Link](host="localhost",
username="root",
password="linus@321",
database="carshowroom"
Insert multiple new
)
cur=[Link]()
rows
while True:
empid=input("Enter New Employee ID:")
empname=input("Enter New Employee Name:")
gender=input("Enter New Employee Gender(M/F):")
dob=input("Enter Employee Date of Birth:(YYYY-MM-DD):")
doj=input("Enter Employee Date of Joining:(YYYY-MM-DD):")
d=input("Enter Empolyee Designation:")
s=int(input("Enter Employee Salary:"))
query="insert into employee values ('%s','%s','%s','%s','%s','%s',%s)\
"%(empid,empname,gender,dob,doj,d,s)
[Link](query)
[Link]()
choice=input("More (Y/N):")
if choice in 'Nn':
break
print("Data inserted successfully.....")
[Link]()
Delete row
import [Link] as c
con=[Link](host="localhost",
username="root", Delete row
password="linus@321",
database="carshowroom"
)
cur=[Link]()
empid=input("EnterEmployee ID to delete it record:")
#query="delete from employee where
empid='{}'".format(empid)
query="delete from employee where empid='%s'"%(empid)
[Link](query)
[Link]()
print("Record successfully deleted")
[Link]()

You might also like