Computer Science
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.
Related Questions
Assertion. One by one the records can be fetched from the database directly through the database connection.
Reason. The database query results into a set of records known as the resultset.
Assertion. The cursor rowcount returns how many rows have been retrieved so far through fetch…() methods.
Reason. The number of rows in a resultset and the rowcount are always equal.
Write code to connect to a MySQL database namely School and then fetch all those records from table Student where grade is ' A' .
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()