0% found this document useful (0 votes)
4 views2 pages

Python SQL Connectivity Assignment

The document consists of an assignment focusing on Python and SQL connectivity, divided into multiple-choice questions, assertion and reasoning, and theory questions. It covers topics such as establishing connections, executing SQL commands, and using methods like fetchone() and fetchall(). The assignment also includes practical tasks and explanations related to database operations in Python.

Uploaded by

trishaygupta16
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)
4 views2 pages

Python SQL Connectivity Assignment

The document consists of an assignment focusing on Python and SQL connectivity, divided into multiple-choice questions, assertion and reasoning, and theory questions. It covers topics such as establishing connections, executing SQL commands, and using methods like fetchone() and fetchall(). The assignment also includes practical tasks and explanations related to database operations in Python.

Uploaded by

trishaygupta16
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

Assignment – Python – SQL Cponnectivity

Section A – Multiple Choice Questions


1 Which of the following is used to establish a connection between Python and MySQL?
a) [Link]()
b) [Link]()
c) [Link]()
d) [Link]()

2 The method used to create a cursor object is:


a) connect()
b) execute()
c) cursor()
d) commit()

3 The commit() method is used to:


a) Create database
b) Save changes permanently
c) Close connection
d) Fetch records

4 What is the correct format to use parameterized query in Python SQL?


a) "INSERT INTO student VALUES(%d, %s)"
b) "INSERT INTO student VALUES('?', '?')"
c) "INSERT INTO student VALUES(%s, %s)"
d) "INSERT INTO student VALUES(#, #)"

5 Which method is used to fetch all rows from the last executed query?
a) fetchone()
b) fetchall()
c) fetchmany()
d) execute()

6 rowcount returns:
a) Total number of tables in database
b) Number of fields in the table
c) Number of rows affected by the last query
d) Total records in the table

7 Which SQL command is used to update data in a table?


a) CHANGE
b) ALTER
c) MODIFY
d) UPDATE

8 What does the execute() method do in SQL connectivity?


a) Connects to SQL
b) Executes SQL command
c) Commits data
d) Closes connection

9 If you want to delete a record from the table using Python, you will use:
a) DELETE FROM table
b) DROP table
c) REMOVE FROM table
d) TRUNCATE table

10 What will fetchone() return if no rows match the query?


a) 0
b) None
c) ""
d) -1

11 What is the purpose of the %s format specifier in SQL queries?


a) To insert numbers
b) To escape special characters
c) To use parameterized values
d) To store strings only

12 Which Python module is used for MySQL connectivity?


a) sqlite3
b) psycopg2
c) [Link]
d) [Link]

13 Which method is used to save the changes made to the database?


a) save()
b) hold()
c) commit()
d) freeze()

14 What is the correct order of operations to execute an insert query?


a) connect → execute → cursor
b) cursor → connect → execute
c) connect → cursor → execute → commit
d) cursor → execute → connect → commit

15 What is the default return type of fetchall()?


a) List of dictionaries
b) Tuple
c) List of tuples
d) Dictionary

Section B – Assertion and Reasoning (2 marks each)

Read the following statements and choose the correct option.


Options:
a) Both A and R are true and R is the correct explanation of A
b) Both A and R are true but R is not the correct explanation of A
c) A is true but R is false
d) A is false but R is true

1 Assertion (A): cursor() is used to create a cursor object in Python SQL connectivity.
Reason (R): Cursor object helps in executing SQL queries
2 Assertion (A): The commit() method is essential after executing SELECT queries.
Reason (R): commit() saves the changes made to the database permanently.

3 Assertion (A): fetchone() fetches the first row of the result set.
Reason (R): fetchone() is used when we want all rows from the result set.

4 Assertion (A): %s is a safe way to insert values into a SQL query in Python.
Reason (R): It helps prevent SQL injection.

Section C – Theory Questions (5 marks each)

1 Explain the steps required to perform database connectivity in Python with an example.
(Include use of connect(), cursor(), execute(), commit().)

2 Write a Python program to perform the following tasks on a table named student (roll, name, marks):
a) Insert a new student record
b) Update marks of a student
c) Delete a student record

3 Discuss the differences between fetchone() and fetchall() with the help of examples. Also, explain the purpose of
rowcount

You might also like