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

MYSQL With Interface Python

Uploaded by

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

MYSQL With Interface Python

Uploaded by

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

My SQL with Python Interface

Inorder to connect a database from within Python, we need a library


that provides connectivity functionality. For that we use
mysqlconnector
Installing MYSQL connector
pip install mysql-connector in the cmd prompt
After installation open Python shell and write
import mysql.connector

1.WAP to connect the database ie test and create the table emp23
with the following field (empno,name and salary) Also insert the row.
2.WAP that read the whole data from the table emp24 and display all
the records.
3.Write a function update to accept the values as parameter and
update the record according to the passed values. Assuming that
values exist in the table.
4.Write a python function to accept the roll no as parameter and find
out whether record present in the table or not.
4.Consider a database 'emp' that has a table 'student' that stores admno of students. Write a
MySQL-Python connectivity to retrieve data, one record at a time, for students with admno less than
2000.
5.Write the python function delete() to delete the record in the student table according to
the roll no passed as parameter.

6.Write a Python-MySQL connectivity program to retrieve all flights from the flights table and
display them sorted by the source city in descending order.
Connect(): function establishes a connection to the MySQL database
from Python application and returns a MYSQL connection object
.connect()function can throw an exception ie Database error if one
of the required parameters is wrong.
Cursor() The cursor method of the MYSQL connection object is used
to work with the database.( which is used to execute SQL
commands.)
mycursor.execute: This method is used to execute the SQL
statement with the database cursor object.
Fetchone():It fetches the single row from the result set.
Fetchmany():This method returns blocks of results according
to a set limit.
Fetchall():It fetches all the rows in a result set.
commit() this command is used to save the changes in the
database.
rowcount():This is a read only attribute and returns the
number of rows that were affected by an execute method.
Commit():This command is used to save the changes in the
database.
For eg db.commit()
Rollback() It is used to cancel all the changes made to the
database and it only works with DML statements.
IF ROW_COUNT() = 0 THEN

#so roll back the transaction

ROLLBACK;

Insert,delete,update
Close(): this method is used to close the database connection
object which we have created.
db.close()
For eg:db.close()
 Username: This is the username that you use to work with
MySQL Server. The default username for the MySQL database is
"root".
 Password: Password is given by the user at the time of installing
the mysql database. If you are using root then you won't need
the password.
➤ Host Name: This is server name or IP address on which MySQL is
running. If you are running on localhost, then you can use localhost,
or its IP, i.e., 127.0.0.0
➤ Database Name: It is the name of the database to which
connectivity is to be established.
1
2

3 Ans: Statement 1: con1.cursor()


Statement 2: mycursor.execute(querry)
Statement 3: con1.commit()

You might also like