0% found this document useful (0 votes)
0 views5 pages

Interface Python

Uploaded by

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

Interface Python

Uploaded by

yallsuckbruh
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 5

CHAPTER 12

DATABASE MANAGEMENT (BOOKBACK UNSOLVED QUESTIONS)


INTERFACE PYTHON WITH MYSQL

1. Design a Python application that fetches all the records from Pet table of menagerie database.
Ans:
import mysql.connector as a
mydb = a.connect(host="localhost",user="root",password="user", database = "menagerie")
cur = mydb.cursor()
run = "select * from PET "
cur . execute(run)
data = cur.fetchall()
for i in data :
print(i)
mydb.close()

2. Design a Python application that fetches only those records from Event table of menagerie
database where type is Kennel.
Ans:
import mysql.connector as a
mydb = a.connect(host="localhost",user="root",password="user", database = "menagerie")
cur = mydb.cursor()
run = "select * from Event where type = 'Kennel' "
cur . execute(run)
data = cur.fetchall()
for i in data :
print(i)
mydb.close()

3. Design a Python application to obtain a search criteria from user and then fetch records based
on that from empl table.
Ans:
import mysql.connector as a
mydb = a.connect(host="localhost",user="root",password="user", database = "portal_express")
name = input("Enter the employee name :- ")
cur = mydb.cursor()
run = "select * from Empl where name = '{}' ".format(name,)
cur . execute(run)
data = cur.fetchall()
for i in data :
print(i)
mydb.close()

4. Write code to connect to a MYSQL database namely School and then fetch all those records from
table Student where grade is 'A'.
Ans:
import mysql.connector as a
mydb = a.connect(host="localhost",user="root",password="portal express", database = "School")
cur = mydb.cursor()
run = "select * from Student where grade = 'A' "
cur . execute(run)
data = cur.fetchall()
for i in data :
print(i)
mydb.close()

5. 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()
Ans:
Rows affected: 1

6. 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()
Ans:
The above program insert a new records in table staff such that person_id and lastname are
input by user . when program has run .

7. Explain what the following query will do?


import mysql.connector
db = mysql.connectar.connect(….)
cursor = db.cursor()
db.execute("SELECT * FROM staff WHERE person_id in {}".format((1,3,4)))
db.commit()
db.close()
Ans:
The above program give us error because above Mysql query ( SELECT * FROM staff WHERE
person_id in (1,3,4) ) do not affect any rows so when db.commit() run then it show error “Unread
result found”.

8. Explain the following 'results' retrieval methods with examples.


A. fetchone ()
B. rowcount ()
C. fetchall ()
Ans:
(A) fetchone() :- The fetchone() method will return only one row from the result set in the form of
tuple containing a record.
(B) rowcount() :- cursor.rowcount() that always return how many records have been retrieved so for
using any of the fetch..() methods.
(C) fetchall() :- The fetchall() method return all the rows from the result set in the form of a tuple
congaing the records.
For example:-
import mysql.connector as a
mydb = a.connect(host="localhost",user="root",password="user", database = " abc")
cur = mydb.cursor()
run = "select * from stud order by salary "
cur . execute(run)
#(A)
data = cur.fetchone()
print(data)
#(B)
cur.rowcount()
#(C)
data = cur.fetchall()
for i in data :
print(i)
mydb.close()

9. What is the significance of connecting Python with MySQL?


Ans: We are able to easily access and work on the database.

10. Why do we use connect () function?


Ans: Because to do anything with database.

11. Explain the transaction keywords used with MySQL-Python connectivity.


Ans:
(i) commit :- MySQLConnection.commit() method sends a COMMIT statement to the MySQL
server, committing the current transaction.
(ii) rollback :- MySQLConnection.rollback reverts the changes made by the current transaction.
(iii) autocommit :- MySQLConnection. Autocommit value can be assigned as True or False to
enable or disable the auto-commit feature of MySQL. By default, its value is
False.

12. Give the significance of using execute () function.


Ans:
The significance of execute() function is execution of queries which are MySQL queries along with
python interface.

13. Differentiate between commit () and rollback () statements.


Ans:
Commit():-
Once a program has completed executing the query with your changes and you want to commit
the changes to the database, then you need to call commit() method on MySQL connection object as
follows.
connection.commit()
Rollback() :-
When one of the transactions fails to execute and you want to revert or undo all your changes, then
you need to call a rollback method of MySQL connection object as follows:
connection.rollback()

14. Which function is used to connect to database?


Ans: connect() function.

15. Which function is used to run the SQL query?


Ans: execute() function

16. Which function is used to read one record from the database?
Ans: fetchone() function.

17. Write a program to display all records in ascending order of their salary.
Ans:
import mysql.connector as a
mydb = a.connect(host="localhost",user="root",password="user", database = " abc ")
cur = mydb.cursor()
run = "select * from emp order by salary "
cur . execute(run)
data = cur.fetchall()
for i in data :
print(i)
mydb.close()

18. Write a program to increase salary of the employee, whose name is "MANOJ KUMAR", by 3000.
Ans:
import mysql.connector as a
db = a.connect(host="localhost",user="root",password="user", database = "abc")
cursor = db.cursor()
cursor.execute("update emp set salary = salary + 3000 where name = 'MANOJ KUMAR' ")
db.commit()
db.close()

19.Write a program to delete the employee record whose name is read from keyboard at execution
time.
Ans:
import mysql.connector as a
db = a.connect(host="localhost",user="root",password="user", database = "abc")
cursor = db.cursor()
name = input("Enter Employee name = ")
cursor.execute("delete from emp where name = { } ".format(name))
db.commit()
db.close()
20. Create a database TESTDB.
Create a table EMPLOYEE with Fields FIRST_NAME, LAST_NAME, AGE, GENDER and
INCOME in TESTDB.
Ans:
import mysql.connector as a
db = a.connect(host="localhost",user="root",password="user", database = "TESTDB")
cursor = db.cursor()
cursor.execute("Create table EMPLOYEE ( FIRST_NAME char(50), LAST_NAME char(50), AGE
integer, GENDER char(1) , INCOME integer )")
db.commit()
db.close()

***************

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy