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

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

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.
1 Crore+ students have signed up on EduRev. Have you? Download the App

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?

Ravi Sharma answered
There are 3 methods used for fetching the records from a database within python.

Checking the options
(A) fetchone( ) - It is a valid method for fetching records from a database within python.
(B) fetchmany( ) - It is a valid method for fetching records from a database within python.  
(C) fetchall( ) - It is a valid method for fetching records from a database within python.
(D) fetchmulti( ) - It is not a valid method for fetching records from a database within python.

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.

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?

Explanation:
A database cursor is a special control structure that facilitates row by row processing of records in a result set. It allows the application to retrieve and manipulate data from a database one row at a time. The cursor acts as a pointer to the current row in the result set, and it can be moved forward or backward to access different rows.

Key Points:
- Row by row processing: A database cursor allows the application to process each row in a result set one at a time. This is useful when dealing with large result sets or when the application needs to perform specific operations on each row individually.
- Result set: A result set is the set of rows returned by a database query. It represents the data that matches the specified conditions in the query. The cursor is used to navigate through this result set.
- Pointer to the current row: The cursor keeps track of the current row in the result set. It allows the application to access the data in the current row and perform operations on it. The cursor can be moved forward or backward to access different rows in the result set.
- Retrieving and manipulating data: The cursor provides methods for retrieving data from the current row, such as getting the value of a specific column. It also allows the application to update or delete the current row, and insert new rows into the result set.
- Connection: A database connection is a communication pathway between the application and the database server. It is used to establish a connection, execute queries, and retrieve data from the database. While a connection is necessary to interact with the database, it is not specifically designed for row by row processing like a cursor.
- Tuple: A tuple is a collection of fields or attributes that represents a single record in a database table. It is not a control structure for row by row processing like a cursor.

Therefore, the correct answer is option 'B', a database cursor. It is specifically designed to facilitate row by row processing of records in a result set.

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?

Jaideep Mehta answered
Connecting a Database from Python

To connect to a database from Python, there are several mandatory arguments that need to be provided. These arguments are necessary to establish a connection and authenticate the user. The correct option for the mandatory arguments required to connect any database from Python is option C, which includes the following arguments: Username, Password, Hostname, and Database Name.

Explanation of the Correct Answer

The correct option C includes the necessary arguments to connect to a database from Python. Let's discuss each argument in detail:

1. Username: The username is the unique identifier of the user who wants to connect to the database. It is used to authenticate the user and determine their access rights and privileges.

2. Password: The password is a secret combination of characters that is associated with the username. It is used as a security measure to ensure that only authorized users can access the database.

3. Hostname: The hostname refers to the address or location of the database server. It can be an IP address or a domain name. The hostname is required to establish a connection to the correct server.

4. Database Name: The database name specifies the name of the database to which the user wants to connect. In a database management system, there can be multiple databases, and the user needs to specify the desired database.

Optional Argument:
In addition to the mandatory arguments mentioned above, there is an optional argument, Port. The port number is used to establish a connection to a specific port on the database server. If the port number is not provided, the default port for the specific database management system is used.

Conclusion

To connect to a database from Python, the mandatory arguments required are Username, Password, Hostname, and Database Name. These arguments are necessary to authenticate the user and establish a connection with the correct database server. Additionally, the Port argument is optional and can be provided to connect to a specific port on the server.

_______ 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?

Gaurav Kumar answered
Checking the options
(A) sqlresult - It is not a valid term related to SQL.  
(B) resultset - It refers to the logical set of records that are fetched from the database when executing an SQL query.
(C) table - It refers to a database object that contains all the data in the table in the form of rows and columns.
(D) tuple - It refers to a single row of a table in an SQL database.
From the options given above, resultset refers to  the set of records that are retrieved  after execution of SQL query over an established  database connection. 
So, the correct answer is (B)

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 2024 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 2024 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

Signup to see your scores go up within 7 days!

Study with 1000+ FREE Docs, Videos & Tests
10M+ students study on EduRev