Download as pdf or txt
Download as pdf or txt
You are on page 1of 5

DOON INTERNATIONAL SCHOOL

CITY CAMPUS,
DALANWALA, DEHRADUN

CLASS XII
COMPUTER SCIENCE
CHAPTER 10:
INTERFACE PYTHON WITH SQL

NOTES
+
SOLVED QUESTIONS
+
Exercise

PREPARED BY :
Ms. Anjali Sharma (PGT COMPUTER SCIENCE)
DOON INTERNATIONAL SCHOOL
CITY CAMPUS,
DALANWALA, DEHRADUN

Interface Python with SQL


We are well aware of the power of programming languages as well the databases. Using
programming languages we can create various applications. We can create applications to
collect data from the user by creating forms. But this data is saved temporarily. If we want
to save this data permanently, it should be stored elsewhere. As we know databases help us
to store and manipulate data very easily and effectively can be used to store data
permanently.

Front End
The softwares through which we fetch input from the user are called Front End Interface.
These can be created using any programming language. Since languages like Python, VB and
Java support easy programming for creating GUI controls are a good choice for creating
Front End Interface.

Back End
The softwares that can store data permanently are called Back End. Since database software
allows users to store and manipulate data easily become a good choice for Back End.
Databases use SQL commands like CREATE, SELECT, INSERT, UPDATE, DELETE to manipulate
data.

Python-SQL connectivity
Python language has in build modules that are used to connect to the databases and also
store and retrieve data and all this can be done using only a few commands and functions.
This is called python-SQL connectivity.

Module For Database Connectivity – mysql.connector


Various Functions for Database Connectivity
1. connect()- this function is used to establish connection to the MySQL server
returns a MySql connection object.
This function has following parameters
User- the name by which you want to login
Password- the password for login.
HostName- the name or IP address of the server to which we want to connect. If you
are running on localhost then same is used as servername. If we want to use IP
address the use 127.0.0.0 for localhost.
Database – name of database we want to connect.
2. cursor()- this function help us to create an object to cursor class through which
we can execute the various sql queries.
DOON INTERNATIONAL SCHOOL
CITY CAMPUS,
DALANWALA, DEHRADUN

3. execute()- this function will help us execute the SQL queries from python and
return results back to python. It is used for traverse through the record of the
database.
4. close()- this function will close the database connection.
5. is_connected()- the function return a Boolean value (True/False) to show if the
python program is connected to the database or not.

Functions for Reading Records from the Database


Once the query has been executed through python the result set is returned to
Python. This resultset can be read using the following functions.
1. fetchone()- this function is used to read the next record from the resulset.
2. fetchall()- this function will read all the records from the resulset. If some
records have already been read the it will fetch the remaining records.
3. fetchmany(n)- this function will fetch the specified number of records from
the resultset
4. rowcount()- this is a read-only attribute. It returns the total number of rows
affected by the query fired through the execute() function.

Some More Functions

commit()-once a program is completed all the changes that were made to the
database are made permanent , once the commit() is used.

rollback()- if the transaction fails and we want to undo any changes made to the
database, we use the rollback() function.

Consider the following table for the python-sql interface program


Database: School

Table: Student

Field Name DataType Description


Rno Integer Roll number
Sname Varchar(50) Student name
Class Varchar(10) Class and sec
Marks Integer Percentage Marks

Assume the table has some records stored in it.


DOON INTERNATIONAL SCHOOL
CITY CAMPUS,
DALANWALA, DEHRADUN

Program 1

Program to display the details of the students of class 12A.


#importing the module
import mysql.connector as con
#creating the database object
db1=con.connect(host=”localhost”,user=”root”,password=””,database=”school”)
#creating the cursor object
cur=db1.cursor()
#running the sql query using execute function
cur.execute(“select * from student where class like ‘12A’ “)
#reading all the records from the resultset rec
rec=cur.fetchall()
#printing one record at a time
for r1 in rec:
print(“ record……..”,r1)
x=cur.rowcount()
print(“ Total Number Of Records fetched are……….”,x)

Program 2

Program to Add a new Record to the table


#importing the module
import mysql.connector as con
#creating the database object
db1=con.connect(host=”localhost”,user=”root”,password=””,database=”school”)
#creating the cursor object
cur=db1.cursor()
#running the sql query using execute function
cur.execute(“insert into student values(21,’Abhinav’,’11E’,85)”)
DOON INTERNATIONAL SCHOOL
CITY CAMPUS,
DALANWALA, DEHRADUN

x=cur.rowcount()
print(“ Total Number Of Records Added are……….”,x)
#add the record permanently
db1.commit()

Program 3

Program to Remove the record of roll number 37


import mysql.connector as con
db1=con.connect(host=”localhost”,user=”root”,password=””,database=”school”)
cur=db1.cursor()
cur.execute(“delete from student where rno=37”)
x=cur.rowcount()
print(“ Total Number Of Records Deleted are……….”,x)
db1.commit()

ASSIGNMENT

1. Write a Python program to increase the marks of all the students of class 11 B by 10.
2. Write a Python program to display records of all the students who are scoring above
90%
3. Write a Python program to remove the records of class 12G
4. Write a Python program to add record of a student with following details
Roll number=51, student name = John, class – 11F, marks=78

You might also like