All Exams  >   Humanities/Arts  >   Computer Science for Class 12  >   All Questions

All questions of Interface Python with SQL Database for Humanities/Arts Exam

Mandatory arguments required to connect any database from Python
  • a)
    Username, Password, Hostname, Database Name, Port.
  • b)
    Username, Password, Hostname.
  • c)
    Username, Password, Hostname, Database Name.
  • d)
    Username, Password, Hostname, Port.
Correct answer is option 'C'. Can you explain this answer?

Gaurav Kumar answered
To create a connection between the MySQL database and python application, the connect( ) function is used. The functions requires 4 parameters:
  • Hostname
  • Username
  • Password 
  • Database name
Checking the options
(A) Username, Password, Hostname, Database  Name, Port. - Port is not a parameter of the function connect( ).
(B) Username, Password, Hostname. - The parameter database name is missing.  
(C) Username, Password, Hostname, Database  Name. - All 4 parameters of the function connect( ) are present. 
(D) Username, Password, Hostname, Port. - Port is not a valid parameter and the parameter database name is missing.  
From the options given above, only option C has all the 4 parameters of function connect( ).
So, the correct answer is (C)

Which method of cursor class is used to insert or update multiple rows using a single query?
  • a)
    cursor.executeall(query, rows)
  • b)
    cursor.execute(query, rows)
  • c)
    cursor.executemultiple (query, rows)
  • d)
    cursor.executemany(query, rows)
Correct answer is option 'D'. Can you explain this answer?

Kunal Ghoshal answered
Explanation:

Understanding the method:
The method used to insert or update multiple rows using a single query in the cursor class is `cursor.executemany(query, rows)`. This method is specifically designed to efficiently handle multiple data rows in a single query execution.

How it works:
- The `executemany` method takes two parameters: the query string and a list of tuples containing the values to be inserted or updated.
- The query string should contain placeholders for the values that will be replaced by the data in the tuples.
- Each tuple in the list represents a row of data to be inserted or updated in the database.

Advantages of using `executemany`:
- Efficiency: By using `executemany`, you can reduce the number of round trips to the database, improving performance.
- Security: This method helps prevent SQL injection attacks by handling the data securely using parameterized queries.
- Convenience: It simplifies the process of inserting or updating multiple rows by allowing you to pass all the data at once.

Example:
python
import sqlite3
conn = sqlite3.connect('example.db')
cursor = conn.cursor()
data = [(1, 'Alice'), (2, 'Bob'), (3, 'Charlie')]
query = "INSERT INTO users (id, name) VALUES (?, ?)"
cursor.executemany(query, data)
conn.commit()
conn.close()
In this example, the `executemany` method is used to insert multiple rows into a SQLite database table called `users` with columns `id` and `name`. Each tuple in the `data` list represents a row of data to be inserted.

Which method of cursor class is used to get the number of rows affected after any of the insert/ update/delete database operation executed from Python?
  • a)
    cursor.rowcount
  • b)
    cursor.getaffectedcount
  • c)
    cursor.rowscount
  • d)
    cursor.rcount
Correct answer is option 'A'. Can you explain this answer?

Explanation:

The correct method of the cursor class used to get the number of rows affected after any insert/update/delete database operation executed from Python is cursor.rowcount.

cursor.rowcount is a read-only attribute that returns the number of rows affected by the last executed SQL statement. It can be used to determine the success of the operation and to get the count of affected rows.

Details:

When a database operation like insert, update, or delete is executed using a cursor object in Python, the database server returns the number of rows affected by that operation.

The cursor.rowcount attribute is then used to access the number of rows affected. This attribute provides the count of affected rows as an integer value.

Here are a few important points regarding the cursor.rowcount attribute:

1. The value of cursor.rowcount is -1 if the number of affected rows is not known or cannot be determined.

2. The value of cursor.rowcount is 0 if the last executed SQL statement did not affect any rows.

3. The value of cursor.rowcount is positive if the last executed SQL statement affected one or more rows. The actual value indicates the number of affected rows.

4. cursor.rowcount returns the number of rows affected by the last executed SQL statement only. It does not provide the count of rows affected by any previous statement.

5. The cursor.rowcount attribute can be used after any insert/update/delete operation to check the success of the operation and to perform further actions based on the count of affected rows.

6. It is important to note that cursor.rowcount is not available for select statements as it only provides the count of affected rows for non-select statements.

In conclusion, cursor.rowcount is the correct method of the cursor class used to get the number of rows affected after any insert/update/delete database operation executed from Python.

_______ is the set of records that are retrieved after execution of SQL query over an established database connection.
  • a)
    sqlresult
  • b)
    resultset
  • c)
    table
  • d)
    tuple
Correct answer is option 'B'. Can you explain this answer?

Resultset
A resultset is the set of records that are retrieved after the execution of an SQL query over an established database connection. It is a fundamental concept in database management systems and plays a crucial role in retrieving, manipulating, and displaying data from a database.

Execution of SQL Query
When an SQL query is executed, it is sent to the database management system (DBMS) for processing. The DBMS analyzes the query, checks for syntax errors, and determines the most efficient way to retrieve the requested data. Once the query is processed, the resultset is generated.

Retrieved Records
The resultset consists of the set of records that match the criteria specified in the SQL query. These records can come from one or multiple tables in the database, depending on the complexity of the query. The resultset can contain zero or more records, depending on whether the query returned any matching data.

Structure of Resultset
The resultset is structured as a table-like structure, with rows and columns. Each row represents a record, and each column represents a specific attribute or field of the records. The columns in the resultset correspond to the columns specified in the SELECT clause of the SQL query.

Manipulating Resultset
Once the resultset is obtained, it can be further manipulated using various operations such as sorting, filtering, grouping, and aggregation. These operations allow for refining the data and extracting specific information from the resultset.

Displaying Resultset
The resultset can be displayed to the user in various formats, such as a table, a list, or a chart, depending on the application or tool used to interact with the database. The user can then analyze, interpret, and utilize the retrieved data for various purposes.

Overall, the resultset is a key component in the retrieval and manipulation of data from a database. It provides the necessary information required to fulfill the user's query and serves as a bridge between the database and the user.

The special control structure that facilitates row by row processing of records in resultset.
  • a)
    Tuple
  • b)
    database cursor
  • c)
    connection
  • d)
    None of these
Correct answer is option 'B'. Can you explain this answer?

Hansa Sharma answered
Checking the options
(A) Tuple - It refers to a single row of a table in an SQL database.
(B) database cursor - It is a database object that retrieves data from the result set one row at a time.  
(C) connection - It refers to creating a link between the MySQL database and the python application.  
(D) None of these
From the options given above, database cursor refers to the special control structure that facilitates row by  row processing of records in resultset. 
So, the correct answer is (B)

Which of the following is invalid method for fetching the records from database within Python?
  • a)
    fetchone()
  • b)
    fetchmany()
  • c)
    fetchall()
  • d)
    fetchmulti()
Correct answer is option 'D'. Can you explain this answer?

Shruti Joshi answered
Invalid Method for Fetching Records from Database in Python: fetchmulti()

Explanation:
The fetchmulti() method does not exist in the Python database API (DB-API) specification. Therefore, it is an invalid method for fetching records from a database within Python.

In Python, connecting to databases and executing queries is typically done using a database module that conforms to the DB-API specification. This specification defines a set of methods and conventions that database modules should adhere to in order to provide a consistent interface for interacting with databases.

The valid methods for fetching records from a database within Python are as follows:

a) fetchone(): This method is used to fetch the next row of a query result set. It returns a single row as a tuple, or None if there are no more rows.

b) fetchmany(size): This method is used to fetch the next set of rows of a query result set. The size parameter determines the number of rows to fetch. It returns a list of tuples, or an empty list if there are no more rows.

c) fetchall(): This method is used to fetch all the remaining rows of a query result set. It returns a list of tuples, or an empty list if there are no more rows.

Invalid Method:
d) fetchmulti(): This method is not a valid method in the Python DB-API specification. It does not exist in the standard set of methods provided by database modules.

Conclusion:
When fetching records from a database within Python, it is important to use the correct and valid methods specified by the DB-API. The fetchmulti() method is not a part of the standard set of methods and should not be used. Instead, the fetchone(), fetchmany(), and fetchall() methods should be used based on the specific requirements of the application.

Which of the following method reflects the changes made in database permanently?
  • a)
    <connection>.done()
  • b)
    <connection>.final
  • c)
    <connection>.reflect()
  • d)
    <connection>.commit()
Correct answer is option 'D'. Can you explain this answer?

Ojasvi Mehta answered
The commit( ) method in python is used to confirm the changes made by the user to a database.
Checking the options
(A) <connection>.done( ) - It is not a valid method used to reflect changes made in the database permanently.  
(B) <connection>.final( ) -  It is not a valid method used to reflect changes made in the database permanently.    
(C) <connection>.reflect( ) -  It is not a valid method used to reflect changes made in the database permanently.    
(D) <connection>.commit( ) -  This method used to reflect changes made in the database permanently .  
So, the correct answer is (D)

Which method of cursor class is used to fetch limited rows from the table?
  • a)
    cursor.fetchsize(SIZE)
  • b)
    cursor.fetchmany(SIZE)
  • c)
    cursor.fetchall(SIZE)
  • d)
    cursor.fetchonly(SIZE)
Correct answer is option 'B'. Can you explain this answer?

Gaurav Kumar answered
Checking the options
(A) cursor.fetchsize(SIZE) - It is not a method of cursor class.
(B) cursor.fetchmany(SIZE) - It returns the number of rows specified by the size argument.
(C) cursor.fetchall(SIZE) - It returns all the rows of a query result.   
(D) cursor.fetchonly(SIZE) - It is not a method of cursor class.
From the options given above, the function cursor.fetchmany(SIZE) is used to fetch limited rows from the table.
So, the correct answer is (B)

Chapter doubts & questions for Interface Python with SQL Database - Computer Science for Class 12 2025 is part of Humanities/Arts exam preparation. The chapters have been prepared according to the Humanities/Arts exam syllabus. The Chapter doubts & questions, notes, tests & MCQs are made for Humanities/Arts 2025 Exam. Find important definitions, questions, notes, meanings, examples, exercises, MCQs and online tests here.

Chapter doubts & questions of Interface Python with SQL Database - Computer Science for Class 12 in English & Hindi are available as part of Humanities/Arts exam. Download more important topics, notes, lectures and mock test series for Humanities/Arts Exam by signing up for free.

Top Courses Humanities/Arts