0% found this document useful (0 votes)
162 views8 pages

XIIth CS Chapter 14 Solutions

The document outlines steps to connect a Python application to a MySQL database and provides example code for fetching records from various tables. It includes explanations of SQL queries for updating and inserting data, as well as fetching records based on user-defined criteria. Additionally, it presents sample outputs for the executed queries and the structure of relevant database tables.

Uploaded by

mohammedashad848
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)
162 views8 pages

XIIth CS Chapter 14 Solutions

The document outlines steps to connect a Python application to a MySQL database and provides example code for fetching records from various tables. It includes explanations of SQL queries for updating and inserting data, as well as fetching records based on user-defined criteria. Additionally, it presents sample outputs for the executed queries and the structure of relevant database tables.

Uploaded by

mohammedashad848
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

ARISE INTERNATIONAL SCHOOL OF EXCELLENCE

STD : XII
SUB : COMPUTER SCIENCE
Chapter 14(Interface Python With Mysql)
Type -A solutions
1.What are the steps to connect to a database from within a Python application ?

Answer :

The steps to connect to a database from within a Python application are as


follows :

Step 1 : Start Python.

Step 2 : Import the packages required for database programming.

Step 3 : Open a connection.

Step 4 : Create a cursor instance.

Step 5 : Execute a query.

Step 6 : Extract data from result set.

Step 7 : Clean up the environment.

2. Write code to connect to a MySQL database namely School and then fetch all those
records from table Student where grade is ' A' .

Answer :

Table Student of MySQL database School

rollno name marks grade section project

101 RUHANII 76.8 A A PENDING

102 GEOGRE 71.2 B A SUBMITTED


rollno name marks grade section project

103 SIMRAN 81.2 A B EVALUATED

104 ALI 61.2 B C ASSIGNED

105 KUSHAL 51.6 C C EVALUATED

106 ARSIYA 91.6 A+ B SUBMITTED

107 RAUNAK 32.5 F B SUBMITTED

import mysql.connector as mysql

db_con = mysql.connect(
host = "localhost",
user = "root",
password = "tiger",
database = "School"
)

cursor = db_con.cursor()

cursor.execute("SELECT * FROM Student WHERE grade = 'A'")


student_records = cursor.fetchall()

for student in student_records:


print(student)

db_con.close()

Output
(101, 'RUHANII', 76.8, 'A', 'A', 'PENDING')
(103, 'SIMRAN', 81.2, 'A', 'B', 'EVALUATED')

3. Predict the output of the following code :

import mysql.connector
db = mysql.connector.connect(....)
cursor = db.cursor()
sql1 = "update category set name = '%s' WHERE ID = %s" %
('CSS',2)
cursor.execute(sql1)
db.commit()
print("Rows affected:", cursor.rowcount)
db.close()
Answer :

Table category
id name

1 abc

2 pqr

3 xyz

Output
Rows affected: 1

SELECT * FROM category ;

+----+------+
| id | name |
+----+------+
| 1 | abc |
| 2 | CSS |
| 3 | xyz |
+----+------+

Explanation
This Python script uses the mysql.connector module to connect to MySQL
database. It updates the 'name' field in the 'category' table where ID is 2 to
'CSS'. The cursor.execute() method executes the SQL
query, db.commit() commits the changes, and cursor.rowcount gives
the number of affected rows. Finally, db.close() closes the database
connection, ending the Python interface with the MySQL database.

4. Explain what the following query will do ?

import mysql.connector
db = mysql.connector.connect(....)
cursor = db.cursor()
person_id = input("Enter required person id")
lastname = input("Enter required lastname")
db.execute("INSERT INTO staff (person_id, lastname) VALUES
({}, '{}')".format(person_id, lastname))
db.commit()
db.close()
Answer :
This Python script uses the mysql.connector package to connect to MySQL database.
Then it prompts users for person ID and last name, inserts these values into the 'staff'
table, using the INSERT INTO SQL statement. After that, it executes the SQL query using
the db.execute method. The changes made by the query are then committed to the
database using db.commit(), ensuring that the changes are saved permanently.
Finally, db.close() closes the database connection, ending the Python interface with the
MySQL database.

5. Explain what the following query will do ?

import mysql.connector
db = mysql.connector.connect(....)
cursor = db.cursor()
db.execute("SELECT * FROM staff WHERE person_id in
{}".format((1, 3, 4)))
db.commit()
db.close()
answer:
This Python script uses the mysql.connector package to connect to MySQL database. It
executes an SQL SELECT query on the 'staff' table, retrieving all rows where the
'person_id' is 1, 3, 4 (using the IN clause). The db.commit() is unnecessary for a SELECT
query since it doesn't modify the database, and db.close() closes the database
connection, ending the Python interface with the MySQL database.
6.
TYPE B SOLUTIONS
1.Design a Python application that fetches all the records from Pet table
of menagerie database.

ANSWER :
import mysql.connector

db_con = mysql.connector.connect(host = "localhost",


user = "root",
passwd = "lion",
database = "menagerie")
cursor = db_con.cursor()

cursor.execute("SELECT * FROM Pet")

records = cursor.fetchall()
for record in records:
print(record)

db_con.close()

Output
('Fluffy', 'Harold', 'cat', 'f', datetime.date(1993, 2, 4), None)
('Claws', 'Gwen', 'cat', 'm', datetime.date(1994, 3, 17), None)
('Buffy', 'Harold', 'dog', 'f', datetime.date(1989, 5, 13), None)
('Fang', 'Benny', 'dog', 'm', datetime.date(1990, 8, 27), None)
('Bowser', 'Diane', 'dog', 'm', datetime.date(1979, 8, 31),
datetime.date(1995, 7, 29))
('Chirpy', 'Gwen', 'bird', 'f', datetime.date(1998, 9, 11), None)
('Whistler', 'Gwen', 'bird', None, datetime.date(1997, 12, 9), None)
('Slim', 'Benny', 'snake', 'm', datetime.date(1996
2. Design a Python application that fetches only those records from Event table
of menagerie database where type is Kennel.

Answer :
import mysql.connector

db_con = mysql.connector.connect(host = "localhost",


user = "root",
passwd = "lion",
database = "menagerie")
cursor = db_con.cursor()
cursor.execute("SELECT * FROM event WHERE type = 'kennel'")

records = cursor.fetchall()
for record in records:
print(record)

db_con.close()

Output
('Bowser', datetime.date(1991, 10, 12), 'kennel', None)
('Fang', datetime.date(1991, 10, 12), 'kennel',

3. Schema of table EMPL is shown below :

EMPL (EMPNO, ENAME, JOB, MGR, HIREDATE, SAL, COMM,


DEPTNO)

Design a Python application to obtain a search criteria from user and then
fetch records based on that from empl table. (given in chapter 13, Table 13.5)

Answer :

Table Empl
EMPN MG HIREDA COM DEPTN
ENAME JOB SAL
O R TE M O

890 1990-12-
8369 SMITH CLERK 800 NULL 20
2 18

SALESMA 869 1991-02- 160


8499 ANYA 300 30
N 8 20 0

SALESMA 869 1991-02- 125


8521 SETH 500 30
N 8 22 0
EMPN MG HIREDA COM DEPTN
ENAME JOB SAL
O R TE M O

MAHADEV MANAGE 883 1991-04- 298


8566 NULL 20
AN R 9 02 5

SALESMA 869 1991-09- 125


8654 MOMIN 1400 30
N 8 28 0

MANAGE 883 1991-05- 285


8698 BINA NULL 30
R 9 01 0

PRESIDE NUL 1991-11- 500


8839 AMIR NULL 10
NT L 18 0

SALESMA 869 1991-09- 150


8844 KULDEEP 0 30
N 8 08 0

MANAGE 883 1991-06- 245


8882 SHIAVNSH NULL 10
R 9 09 0

888 1993-01- 110


8886 ANOOP CLERK NULL 20
8 12 0

856 1992-12- 300


8888 SCOTT ANALYST NULL 20
6 09 0

869 1991-12-
8900 JATIN CLERK 950 NULL 30
8 03
EMPN MG HIREDA COM DEPTN
ENAME JOB SAL
O R TE M O

856 1991-12- 300


8902 FAKIR ANALYST NULL 20
6 03 0

888 1992-01- 130


8934 MITA CLERK NULL 10
2 23 0

import mysql.connector

db_con = mysql.connector.connect(host = "localhost",


user = "root",
passwd = "fast",
database = "employeedb")
cursor = db_con.cursor()

search_criteria = input("Enter search criteria : ")


sql1 = "SELECT * FROM EMPL WHERE {}".format(search_criteria)
cursor.execute(sql1)

records = cursor.fetchall()
print("Fetched records:")
for record in records:
print(record)

db_con.close()

Output
Enter search criteria : job = 'clerk'

Fetched records:

(8369, 'SMITH', 'CLERK', 8902, datetime.date(1990, 12, 18), 800.0, None, 20)
(8886, 'ANOOP', 'CLERK', 8888, datetime.date(1993, 1, 12), 1100.0, None, 20)
(8900, 'JATIN', 'CLERK', 8698, datetime.date(1991, 12, 3), 950.0, None, 30)
(8934, 'MITA', 'CLERK', 8882, datetime.date(1992, 1, 23),

You might also like