0% found this document useful (0 votes)
106 views12 pages

Python MySQL Integration Guide for CBSE

Uploaded by

aliyatasneem122
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)
106 views12 pages

Python MySQL Integration Guide for CBSE

Uploaded by

aliyatasneem122
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

Class 12 CBSE Interface Python With MySQL

Interface Python With MySQL

CBSE NCERT

CLASS 12 COMPUTER SCIENCE CBSE BOARD


Class 12 CBSE Interface Python With MySQL
Interface Python with MySQL

✓ When you design real-life applications, you are bound to encounter situations
wherein you need to manipulate Data Stored in a database through an application
designed by you.
✓ In order to connect to a database from within Python , you need a Library that
provides connectivity functionality.
Example: mysql connector

CLASS 12 COMPUTER SCIENCE CBSE BOARD


Class 12 CBSE Interface Python With MySQL

❖ Before we connect Python with database i.e. MySQL , we need to build a bridge
between Python and MySQL.
❖ To build this bridge we need a connector called “[Link]”.
❖ We can install [Link] using following method:
At Command Prompt :
Type “pip install mysql-connector” and press Enter

CLASS 12 COMPUTER SCIENCE CBSE BOARD


Class 12 CBSE Interface Python With MySQL

Steps for Creating Database Connectivity

Start Python

Import the package [Link] import [Link]

Open a connection to a database Using connect( ) function

Create a cursor instance Using cursor( )

Execute a query Using execute( ) function

Extract data from the result set Using fetchall( ) , fetchone( ) , fetchmany( ) functions

Clean up the environment Using close( ) function

CLASS 12 COMPUTER SCIENCE CBSE BOARD


Class 12 CBSE Interface Python With MySQL

Steps for Creating Database Connectivity


1. Import [Link] Package:
First of all you need to import [Link] package in your python scripts.

import [Link]

OR
import [Link] as sqlcon

2. Open a connection to MySQL Database:


Next you need to establish connection to a MySQL database using connect( ) function of [Link]
package.

<connection-Object>=[Link](host=<host-name>,user=<username,
passwd=<password>[,database=<database-name>])

import [Link] as sqlcon


con=[Link](host=“localhost”,user=“root”,passwd=“12345”,database=“school”)

CLASS 12 COMPUTER SCIENCE CBSE BOARD


Class 12 CBSE Interface Python With MySQL

Steps for Creating Database Connectivity

CLASS 12 COMPUTER SCIENCE CBSE BOARD


Class 12 CBSE Interface Python With MySQL

Steps for Creating Database Connectivity

3. Create a Cursor instance:


A database cursor is a useful control structure that facilitates the row by row processing of records in the resultset.

You create an instance of cursor using cursor( ) function as per following syntax:
<cursorobject>=<connectionobject>.cursor( )

import [Link] as sqlcon


con=[Link](host=“localhost”,user=“root”,passwd=“12345”,database=“school”)
cursor=[Link]( )

CLASS 12 COMPUTER SCIENCE CBSE BOARD


Class 12 CBSE Interface Python With MySQL

Steps for Creating Database Connectivity

4. Execute SQL Query:


Once you have created a cursor , you can execute SQL query using execute( ) function with cursor object as
per following syntax:

<cursorobject>.execute(<sqlquery>)

import [Link] as sqlcon


con=[Link](host=“localhost”,user=“root”,passwd=“12345”,database=“school”)
cursor=[Link]( )
[Link](“select * from data”)

CLASS 12 COMPUTER SCIENCE CBSE BOARD


Class 12 CBSE Interface Python With MySQL
Extract Data from Resultset

To extract data from cursor following functions are used:


❑ fetchall( ):It will return all the rows of a query result. It returns all
the rows as a list of tuples.
❑ fetchone( ):It return one record from the result set as a [Link]
time it will return the first record, next time it will fetch the next
record and so [Link] no more record it will return None.
❑ fetchmany( ): It will return n-number of records as list of tuple. If
no more record it will return an Empty Tuple.
❑ rowcount: It will return number of rows retrieved from the cursor
so far.

CLASS 12 COMPUTER SCIENCE CBSE BOARD


Class 12 CBSE Interface Python With MySQL
Parameterised Queries

Getting Input from User

Such queries are called parametrised queries.

CLASS 12 COMPUTER SCIENCE CBSE BOARD


Class 12 CBSE Interface Python With MySQL
Parameterised Queries Getting Input from User

.format() method :

Syntax:
{ }{ }.format(value1,value2)

CLASS 12 COMPUTER SCIENCE CBSE BOARD


Class 12 CBSE Interface Python With MySQL

CBSE SAMPLE PAPER 2024

CLASS 12 COMPUTER SCIENCE CBSE BOARD

You might also like